Python while Loop

The Python while Loop is used to repeat a block of statements for a given number of times until the given condition is False. A while loop in Python starts with the condition; if the condition is True, then statements inside it will be executed. If the given condition is false, it won’t execute at least once, which means it may execute zero or more times, and the syntax of it is:

Python while loop syntax

The Python While Loop Syntax is as follows:

while (Condition or Expression):
   statement 1
   statement 2
    ………….
# This is the statement Outside of it but inside the Program

Once you hit enter after a semicolon, the While loop will start the next statement with Tab space, and this Tab space acts as curly braces ({ }) in other programming languages.

First, the compiler will check the expression inside the Python While Loop. And if the condition is True, then the statement or group of statements under this block will execute. If the expression is False, the compiler will come out of that code block and execute other statements outside.

Flow Chart

This flow chart will explain to you Python while loop Visually and perfectly.

While Loop FLOW CHART

The Python while loop will check for the condition at the beginning of it.

  1. If the condition evaluates to True, it will execute the code inside.
  2. Next, we must use Arithmetic Operator inside the while loop to increment and decrease the value.
  3. After the value increments, it will again check the expression. As long as the condition is met, the statements in it will be executed.
  4. If the expression becomes false, then it will exit from it.

Python while loop Example

Let us see the while loop example for better understanding. First, we created a variable called total, which is initialized to 0.

This program allows the user to enter an integer below 10. And the second line of the Python code stores the user-given integer in a variable number. Using this number, the control will add those values to sum up to 10.

In the next line, we used to test the expression. If the condition result is true, the number adds to the total. Otherwise, it will exit from the Python while loop. We also used the + operator to increment the number value (number = number +1). After increment, the process repeats until the condition results as False.

There is a print statement outside of it. It executes when the condition is either True or False.

total = 0
num = int(input(" Please Enter any integer below 10:  "))
while (num <= 10):
    total = total + num
    num = num + 1
print(" Total is: ", total)

We are going to enter the number 5. It means, total = 5 + 6 + 7 + 8 + 9 + 10 = 45

Please Enter any integer below 10: 5
Total is: 45

Python while loop Else Example

Python programming allows us to use the else statement with while loop statements, which works like the If Else statement.

  • The statements inside the code block will execute if the condition is True.
  • If the condition is False, the statements inside the Else clause will execute. When we use the Break statement to break the execution of the loop, then the Else block will not be executed. Please refer If Else and Break statements.

This Python While loop else program allows users to provide an integer below 10. Using this number, the control will add those values up to 10.

The condition (number<= 10) checks whether the number is less than or equal to 10. If the expression result is true, the number is added to the total.

Next, We used + to increment the number value in the Python while loop. After increment, the process will repeat until the condition returns False.

In the next line, we used the print function inside it, and this line will display the value inside the total for every iteration.

If the condition evaluates to False, the print function inside the else block will execute the message.

total = 0
number = int(input(" Please Enter any integer below 10:  "))
while (number <= 10):
    total = total + number
    number = number + 1
    print(" Value of Total : ", total)
else:
    print(" You Value is Greater Than 10 ==> This is from Else Block ")

Here we entered the value as 5; when it reaches 10, the condition will fail. So it will enter into the else block and print the statements inside the Else clause.

Python While Loop with else

Python Infinite while Loop

If you forget to increment or decrement the value inside the Python while loop, it will repeatedly execute infinite times (also called an infinite loop).

x = 1
while x < 10:
    print("Value = ", x)
Value = 1
Value = 1
Value = 1
Value = 1
Value = 1
Value = 1
....
....
...

Here x is always 1, and x is always less than 10, so the code will execute infinite times. Now, let us add the Arithmetic operator (x = x +1) inside of the above example.

# Infinite Solution

x = 1
while x < 10:
    print(x)
    x = x + 1 # To increment X value

When it reaches 10, the expression will fail.

1
2
3
4
5
6
7
8
9