Wednesday, August 3, 2022

How to create While loop in Python?

There are two primitive loop commands in Python and these are:
  • while loops and
  • for loops.
Using while loop you can execute the set of indented statements as long as a condition is True. The general form of while loop can be written as:
while condition:
indented statements
check condition
In the 1. code line the while command is given follwed by the condition. As long as the condition is Ture the indented statements will be executed. At the end of the indented statements the condition is checked and if the condtion is False the while loop is terminated.
Example 1 Write simple while loop that will print first 20 numbers. The condition will be checked at first line.
number = 0
while number < 21:
print("number = ", number)
number += 1
In the 1. code line the variable named "number" is created and value 0 is assigned to a variable. In the 2. code line the while loop is created with condition that the number is less than 21. Since we have to build the program that will print numbers from 0 to 20 and including 20 the while loop will be terminated after the number reaches value of 20. Since initially the variable number is equal to 0 the condition is True and the indented block inside the while loop begins execution. In the 3. code line the value of the variable number is printed using print function. Then in 4. codel line the value of variable "number" is enlarged by 1 and this is done in each iteration. The 3. and 4. code line is repeated until the condtion number < 21 is False. Then the execution of the program is terminated. After running the previous code block the following output will be generated.
number =  0
number = 1
number = 2
number = 3
number = 4
number = 5
number = 6
number = 7
number = 8
number = 9
number = 10
number = 11
number = 12
number = 13
number = 14
number = 15
number = 16
number = 17
number = 18
number = 19
number = 20
The while loop can be interpreted as follows:
  • Evaluate the condition, resulting in True or False.
  • If the condition is False, exit the while statement and continue the execution at the next statement.
  • If the condition is True, execute the body and go back to the frist step.
From the given flow it can be seen why is it called a loop since the third step loops back around to the top. Each time the body of the loop is executed it is called an iteration. In Example 1 we had 20 iterations which means that the body was executed 20 times.
As you can see from the while loop body in the last code line the number variable is incremented in each iteration. Then in the next iteration the updated value of the number variable is compared to 21. IT must be less then 21 in order to begin next iteration. If the value of the number variable is not updated in each iteration the while loop wood iterate indefinitly or until we terminate the execution. So the number variable changes each time the loop executes and controls the loop execution. This variable is called iteration variable.

No comments:

Post a Comment