Python while loop

Python while Loop
while loop
It works as long as the given condition is true.
Syntax:
while ():
// Statements

Example 1:
# Printing the no.1 to 10
ctr=1 #initialization

print(“While Loop Demo”)
while (ctr<=10): #condition
print (ctr)
ctr = ctr +1 #incrementing part

Example 2:
#Printing the No.s from 1000 to 100

ctr=1000
print(“While Loop Demo”)
while (ctr >=100):
print (ctr)
ctr = ctr – 100
else:
print(“else part will execute when the condition becomes false”)

Note: Else is an optional and will execute when the condition given the while loop fails.

Win Corporate Training
website: http://www.wincorptrg.com
View My Profile

Advertisements