Python While Loop

The While loop is used for iteration. In each iteration, it evaluates the truth expression just like the If statement. If the condition evaluates to true, then the statement inside the loop is executed and control goes to the next iteration. If the condition evaluates to false, then the loop ends and control goes out of the loop. The while statement can also have an optional else: clause. The else: clause is executed, if the while statement completes normally, that is if a break statement is not executed.

The reserved word while begins the while statement. The condition determines whether the body will be executed. A colon (:) must follow the condition. The block is a block of one or more statements to be executed as long as the condition is true.

Syntax of While loop

while condition : block

Example of While loop

>>> count = 1
>>> while count <= 5:
	count += 1


Python while loop

Output of the above code
Python nested if statement

Read more articles

General Knowledge

Learn Popular Language