There are two primitive loop commands in Python and these are:
Example 1 Write simple while loop that will print first 20 numbers. The condition will be checked at first line.
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.
- while loops and
- for loops.
while 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.
indented statements
check condition
Example 1 Write simple while loop that will print first 20 numbers. The condition will be checked at first line.
number = 0In 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.
while number < 21:
print("number = ", number)
number += 1
number = 0The while loop can be interpreted as follows:
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
- 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.
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.