Iteration: Repeating Code
Loops allow us to execute a block of code repeatedly. The while loop continues executing its code block as long as its controlling condition remains True.
Syntax
python while condition: # Code block runs repeatedly statement_1 # CRUCIAL: Must update variable related to condition
Example: Simple Counter
We must ensure the condition eventually becomes False to avoid an infinite loop.
python count = 1
while count <= 5: print(f"Current count: {count}") count += 1 # Update the control variable
print("Loop finished.")
Output:
Current count: 1 Current count: 2 Current count: 3 Current count: 4 Current count: 5 Loop finished.