Python while Loop

The Python while Loop is used to repeat a block of statements for given number of times, until the given condition is False. A while loop in Python start 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 time and the syntax of it is:

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 the enter after semicolon, python 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 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.

Python while Loop Flow Chart

This flow chart will explain you Visually and perfectly

WHILE LOOP FLOW CHART

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

  1. If the condition evaluates to True then it will execute the code inside it.
  2. Next, we have to use Arithmetic Operator inside the Python while loop to increment and decrement 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 python while loop example for better understanding. First, we created a variable called total and it 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 variable number. Using this number, compiler 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 then the number adds up to the total, otherwise, it will exit from the Python while loop. We also used + operator to increment the number value (number = number +1). After increment, the process repeats until the condition results False.

There is a print statement outside of it. It execute 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 number as 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

The Python programming allows us to use the else statement with while loop statements as well and it works like If Else statement.

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

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

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

Next, We used + to increment number value. After increment, the process will repeate until the condition returns False.

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

If the condition evaluates to False, 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 value as 5, when it reached 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

Infinite While Loop

If you forgot to increment or decrement the value inside the Python while loop, then 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 go on execute infinite times. Now, let us add 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 condition will fail.

1
2
3
4
5
6
7
8
9