Python While Loops

About Python while loop

In Python, the 'while' loop is used to execute a block of code repeatedly as long as a given condition is true.

In while loop, the condition is evaluated before each iteration. If the condition is True, the block of code inside the loop is executed.

If the condition is False, the loop will terminate and stop to execute the block of code.

It's important to ensure that the condition in a while loop will eventually become False. Otherwise, the loop will continue indefinitely.

Typical syntax of 'while' loop

# initializling a variable: Before the loop starts, you need to typically initialize one or more variables.

while condition:: 'while' keyword starts the loop and 'condition' will be replaced by a condition. The condition is evaluated. If it is True, the code block inside the loop is executed. If it is False, the loop stops. The condition is followed by a colon (:) which is part of the syntax.

# code block to be executed:If the condition is True, the defined code block is executed.

# update the value of the variable: When the loop continues to execute, often a change to the variable is needed to check the condition repeatedly until the condition becomes False.

Example of while loop

Following is a typical example of 'while' loop:

Example of 'while' loop with a condition
Output of the above example
The number is: 0
The number is: 1
The number is: 2
The number is: 3
The number is: 4
                                

Explanation of example:

count = 0: This line initializes an int variable named 'count' with value 0.

while count < 5: This line defines the 'while' loop with a condition. As long as the value of count is less than 5, the while loop will continue to iterate.

print("The number is:", count): The code block under 'while' loop will be executed as long as the value of count is less than 5.

count += 1: The line increments the value of count by 1.

'while' loop with multiple conditions

In Python, 'while' loop can have multiple conditions.

Multiple conditions are joined by logical operators 'and' or 'or' as demonstrated in the following example:

Example of 'while' loop with multiple conditions
Output of the above example
x: 10, y: 0
x: 9, y: 1
x: 8, y: 2
x: 7, y: 3
x: 6, y: 4
x: 5, y: 5
x: 4, y: 6
x: 3, y: 7
x: 2, y: 8
x: 1, y: 9
                                

Explanation of example:

x = 10 and y = 0 are two int variables named 'x' with value 10 and 'y' with value 0.

while x > 0 and y < 10:: This line defines the 'while' loop with two conditions joined by logical operator 'and'. As long as both conditions are True, the 'while' loop will continue to iterate.

print(f"x: {x}, y: {y}"): The code block under 'while' loop will be executed as long as the both conditions are True.

x -= 1: The line decrements the value of x by 1.

y += 1: The line increments the value of x by 1.

'while' loop with 'else' block

In Python, 'while' loop takes an 'else' block.

The code inside the 'else' block runs once when the condition of 'while' loop is False.

Example of 'while' loop with 'else' block
Output of the above example
The number is: 5
The number is: 6
The number is: 7
The number is: 8
The number is: 9
While loop completed & else block runs.
                                

Explanation of example:

number = 5: This line initializes an int variable named 'number' with value 5.

while number < 10: This line defines the 'while' loop with a condition. As long as the value of 'number' is less than 10, the while loop will continue to iterate.

print("The number is:", number): The code block under 'while' loop will be executed as long as the value of 'number' is less than 10.

number += 1: The line increments the value of 'number' by 1.

else: The 'else' block runs once the iteration of the 'while' loop is completed.

print("While loop completed & else block runs.") It prints the message when the 'else' block runs.

'while' loop with nested loop

In Python, 'while' loop can have nested loops (loop within a loop). You can use 'while' or 'for' loop as nested loop.

The nested 'while' loops are used to check more conditions as demonstrated in the following example:

Example of 'while' loop with nested loop
Output of the above example
1 1
1 2
2 1
2 2
3 1
3 2
                                

Explanation of example:

i = 1: This line initializes an int variable named 'i' with value 1.

while i < 4: This line defines the 'while' loop with a condition. As long as the value of 'i' is less than 4, the loop will continue to iterate.

j = 1: This line initializes an int variable named 'j' with value 1 at the time of each iteration.

while i < 4: This line defines the nested 'while' loop with a condition. As long as the value of 'j' is less than 3, the while loop will continue to iterate.

print(i, j): The code block under nested 'while' loop will be executed as long as the value of 'j' is less than 3.

j += 1: The line increments the value of 'j' by 1.

i += 1: The line increments the value of 'i' by 1.

'while' loop with break

In Python 'while' loop, break is used to terminate the loop and pass the control flow to the next statement as demonstrated in the following example:

Example of 'while' loop with break
Output of the above example
Counting: 1
Counting: 2
Counting: 3
Counting: 4
Counting: 5
                                

Explanation of example:

count = 1: This line initializes an int variable named 'count' with value 1.

while count < 10: This line defines the 'while' loop with a condition. As long as the value of 'count' is less than 10, the while loop will continue to iterate.

print("Counting:", count): The code block under 'while' loop will be executed as long as 'break' does not terminate the loop.

if count == 5:: This line defines a condition to check if the count value is equal to 5.

break If the count value is equal to 5, 'break' terminates (stops) the 'while' loop.

count += 1: The line increments the value of 'count' by 1.

'while' loop with continue

In Python 'while' loop, continue is used to skip the rest of the code inside the current iteration and move to the next iteration of the loop as demonstrated in the following example:

Example of 'while' loop with continue
Output of the above example
Odd number: 1
Odd number: 3
Odd number: 5
Odd number: 7
Odd number: 9
                                

Explanation of example:

count = 0: This line initializes an int variable named 'count' with value 0.

while count < 10: This line defines the 'while' loop with a condition. As long as the value of 'count' is less than 10, the while loop will continue to iterate.

count += 1: The line increments the value of 'count' by 1.

if count % 2 == 0:: This line defines an if condition to check if the count value is an even number.

continue If the count value is an even number, 'continue' skips the rest of the code inside the current iteration and move to the next iteration of the loop.

print("Odd number:", count): The code block under 'while' loop will be executed as long as 'continue' does not skip the iteration.

Python Compiler For Live Practice

Practice the above Python code in the following Python Editor. Create more Python while loop statements in the following Python code editor and click Execute button to check the output. Output will be displayed in Python Code Output section.

Python logo Python Editor
Python Code Output