Python While Loop | Explained

The Python programming language supports some control flow statements like the other languages. These control flow statements include if-else, for loop, while loops, etc. The “while” loop is one of the control flow statements that execute the code block for some time when the condition is “True”. In a program, it is used to repeat a particular block of code several times.

This write-up will provide a detailed analysis of the Python “while” loop with multiple examples. The following aspects are elaborated on in this article on Python:

So let’s begin!

What is While Loop in Python?

Python while loop iterates over the code block only if the condition remains “True”. The “while” loop syntax is shown in the below snippet::

while expression:
    Body of while

In the above syntax:

  • The “while” loop expression condition is checked at the start, and if the condition becomes “True”, then the body of the condition will be executed.
  • The statements of the body must be placed with some increased indentation.

Let’s look at the first examples to understand how the while loop works:

Example 1: Simple While Loop in Python

In the following examples, the “while” loop executes the statement until the given condition is satisfied.

Code:

# while loop program
x = 0

while (x < 5):
    x = x + 1
    print("Python Guide")

In the above code:

  • A variable “x” is created and assigned a value of “0”.
  • The “while” loop executes its body statement only if the count value is less than “5”.
  • The count value will be incremented every time; if we do not increment the count value, the program will run infinite times.

Output:

The output above shows the value of the print statement (five times) based on the conditions defined within the while loop.

Example 2: While Loop With break Statement

In the example given below, the “while” loop is used along with the “if” statement and ”break” statement.

Code:

x = 0
#using while loop
while x < 7:
  print(x)
  if x == 5:
    break
  x += 1

In the above code:

  • The counter value is assigned to be “zero” at the start.
  • The “while” loop statement becomes “True” until the given condition is satisfied. If the value is smaller than “7”, then the while loop executes the statements present within the body.
  • The “break” statement is written inside the “if” block that will break the sequence and return the result on the screen.

Output:

In the above output, the value is displayed until the break statement is executed.

Example 3: While Loop With else Statement

In the following code, the “while” loop is used along with the “else” statement.

Code:

x = 0
while x < 6:
  print(x)
  x += 1
else:
  print("Number is Greater than 5")

In the above code:

  • In the above code block, a while loop is used, which will execute until x<6.
  • The statement written inside the “while” block executes until the input condition is “True”.
  • The “else” block executes when the “while” loop condition is not satisfied.

Output:

The above output authenticates the working of the while loop with the else statement.

Example 4: Multiplication Table Using While Loop

In the given code below, the “while” loop is used to print the table of any input number.

Code:

number = 4       
value = 1     
#using while loop
while value <= 10:
    result = number * value     
    print (number, 'x', value, '=', result)     
    value += 1

In the above code:

  • The “number” variable and the counter variable “value” is assigned an integer value.
  • The “while” loop condition becomes “True” for values less than “10”.
  • The given number is multiplied with a counter on each iteration of the “while” loop and prints the result on the screen.

Output:

In the above output, the multiplication table of “4” is printed using a “while” loop.

That’s all, folks!

Conclusion

In Python, the “while” loop executes a particular block of code only if the condition becomes “True”. The “while” loop controls certain code blocks according to defined conditions. The break statement is used along with the combination of the “while” loop to break the sequence at a certain point. The “while” loop is also used with the “else” statement to combine multiple conditions. This article presented a detailed overview of Python’s “while” loop with numerous examples.