python / beginner
Snippet
The While Loop
A while loop repeatedly executes a block of code as long as a specified condition remains true.
snippet.py
1
2
3
4
counter = 0while counter < 3:print(counter)counter += 1
Breakdown
1
while counter < 3:
Checks if the variable 'counter' is less than 3 before each iteration.
2
print(counter)
Outputs the current value of the counter to the console.
3
counter += 1
Increments the counter by 1 to eventually satisfy the loop exit condition.