Linux

Python while loop

In previous page you have seen Python for loop, which executes a set of statements a definite number of times specified by the programmer
Some time it is unpredictable to determine, in how many iterations program complete it's task. The loop eventually completes its work, but only when a condition changes. In some situation you use infinite loop and inside the loop some condition decides when to terminate the loop. . This type of process is called conditional iteration. In this page, we explore the use of the Python while loop to describe conditional iteration

Syntax of Python while loop
while <condition>:
	<sequence of statements>
At least one statement must present in the loop body that update the variable that affects the condition lead to termination of Python while loop. Sentry variable or loop control variable control the while loop. Let us discuss the first example.

Example 1 :

acc = 123456
num = int(raw_input("Enter the account number\t"))
while acc != num:
   print "Wrong number "
   num = int(raw_input("Enter the right account number\t"))
print "\n*********"
print "You have Entered right number"


Out-put:
Python while loop
while loop
In above program acc is compared to num, acc is called control variable. When you enter the wrong account number, program again ask for right account number until you type right number. So this is the power of while loop, unless you enter right number program will never end.

Count Control with a Python while Loop

A while loop can also be used for a count-controlled loop. The next two code segments show the same summations with a for loop and a while loop, respectively.

Example 2 :

sum = 0
for count in xrange(1,10):
   sum = sum+count
   print sum,
print "\n"
sum = 0
count = 1
while (count < 10):
   sum = sum + count
   print sum,
   count= count + 1

Out-put:
Python count controlled while loop
count controlled loop
Both code produce same result. But while loop code segment is noticeably more complex, containing extra statements. The Count is the loop control variable must be explicitly initialized before the loop header. Although, use of Pythonwhile loop brings more labour for programmer, however, you will soon see problems for which Python while loop is the only solution.

Python while True loop and the break statement

Although the while loop is little bit complicated to write correctly, But in next program you will see, use of while loop with break statement.

Example 3 :

sum = 0
while True:
   data = int(raw_input("Enter the data or press 0 to quit :"))
   if data == 0:
      break
   sum = sum+data
print "Sum is ", sum

Out-put:
Python while True loop and the break statement
while True loop and the break statement

In above program while True: means loop's entry condition is the Boolean value True. It remains infinite loop until you pressed 0. When use press 0 break statement is called and the break statement will cause an exit from the loop. The thing to note is that the loop would execute at least one time. Consider if you don't want to use True and break statement you can set your own logic. Let us discuss another example.

Example 4 :

sum = 0
flag = 1
while flag == 1:
   data = int(raw_input("Enter the data or press 0 to quit :"))
   if data == 0:
      flag =0
   sum = sum+data
print "Sum is ", sum

Out-put:
While loop alternate flag
while example

In above program same thing has been done as previous program does. In this program we use flag variable which is initially set to 1, remain 1 until you press 0 to quit. When you press 0, flag value set to 0 which break the while loop automatically.





admin