While loop - Learn Python 3 - Snakify

Lesson 6. While loop




1/12. While loop

while loop repeats the block of statements many times while some condition evaluates to True. When the condition is evaluated to False, the block of statements is executed no more, and while loop is ended. The condition is given before the loop body (before the block of statements) and is checked before each execution of the loop body. Typically, the while loop is used when it is impossible or inconvenient to determine the exact number of loop iterations in advance.

Primarily, Python checks the condition. If it is False, the loop is terminated and the program performs instructions that go after the while loop.

If the condition is True, the loop body is executed, and then the condition is checked again. This continues while the condition is True.

Once the condition becomes False, the loop terminates and the program performs instructions that go after the while loop.
# while some condition:
#     a block of statements

i = 1
while i <= 5:
    print(i)
    i += 1