2.4.2.1 while Loop
Python Programming
A while loop runs as long as a condition is true. It checks the condition before each iteration and stops running when the condition is no longer true.
python
# Syntax of while loop while condition:
# code to run while the condition is trueExample: Add 1 to a number until it reaches 10.
python
number = 1
while number < 10:
print(number)
number += 1Explanation: In this example, the code starts with a number set to 1. The 'while' loop checks if the number is less than 10. If it is, the program prints the current value of the number and then adds 1 to it. This loop continues until the number reaches 10, at which point the loop stops running.