Python While Loop

by | Oct 14, 2021 | Python Programming

In this tutorial, we are going to understand the while loop in python.

Python While Loop

What is Python While Loop?

The while loop in python is used to execute a set of statements until the given test expression or condition is true. 

Syntax of python while loop.

while test_condition:
statements

As we can see in the above syntax, we are first using the while keyword to implement the while loop, and we are giving test conditions, and in the while loop, we are giving the body of while with some statements.

The loop will test the condition, and it will run statements in the body only if the giving condition is true, just like for loop while in python again checking the condition. Once the condition gets false it will exit from the loop.

Do While loop

Example of Python while loop

# Program to print the numbers until the given condition is true.


i = 1 # Declaring variable

while i < 10: # implementing while loop with
print(i)  # printing the variable
i += 1 # Adding increment in i by +1

When we run the following program, we will get the below output

1
2
3
4
5
6
7
8
9

Note: remember to add increment in i, or else the loop will continue forever.

Same as numbers, we can also print strings inside a while loop.

Example of while loop using string

# Program will print the given string as long as the given condition is fulfilled as true.

i=1

while i < 5:
print("Hello Python")
i += 1

The output of the above program:

Hello Python
Hello Python
Hello Python
Hello Python

As in python index starts from 0, it is only printing strings for 4 times only. Because in python calculations begin to like :

Nested while loops

In nested while loop, we can add while loop within while loop as a body of while loop.

while test_condition:
while test_condtion:
statements
statements

Let see an example of nested while loops.

i=1
while i<=3 :
    print(i,"Outer loop")
    j=1
    while j<=3:
        print(j,"Inner loop")
        j+=1
    i+=1;

When we execute this program, we will get the following result.

The output of nested while loop

1 Outer loop
1 Inner loop
2 Inner loop
3 Inner loop
2 Outer loop
1 Inner loop
2 Inner loop
3 Inner loop
3 Outer loop
1 Inner loop
2 Inner loop
3 Inner loop

While loop with else statement 

Using else statements in the while loop can run a set of code once when the given condition is no longer true.

Example of While loop with else statement.

i = 1
while i < 6:
  print(i)
  i += 1
else:
  print("i is no longer less than 6")

The output of following the program.

1
2
3
4
5
i is no longer less than 6

Loop Control Statements

Loop control statements mean a conditional statement as a body of a while loop. We can add multiple condition statements using a conditional statement based on the loop sequence of the loop.

Syntax:

while test_condition:
statements
if condition:
statements

Let see an example of the loop control statement:

# Program will print the string as while loop sequence gets value 5 in 10 numbers.

number = 1
while number < 10:
if number == 5:
print("Here if conditional statement is executed")
print(number)
number += 1
print("End of the loop")

When we run this program, we will get the following output:

1
2
3
4
Here if conditional statement is executed
5
6
7
8
9
End of the loop

In the code above program, we can see while loop executes the statements in between the while loop body and when every while loop is getting 5 as a value. It will go to if conditional statement, and the string will be executed once the value of the number is 5.

break Statement

With the break statement in the while loop, we can stop the while loop even after the given condition is true:

Example:

# Program will stop the loop as soon as while loop sequence will get value 3

i = 1

while i < 6:
print(i)
if i == 3:
break
i += 1

Output:

1
2
3

You can learn more about python break statements from here.

continue Statement

The Python continue statement immediately terminates the current loop iteration.

Example:

n = 5
while n > 0:
n -= 1
if n == 2:
continue
print(n)
print("Loop ended")

Output:

4
3
1
0
Loop ended

Learn more about python continue statements from here.

One-Line while Loops

As with an if statement, a while loop can be specified on one line. If there are multiple statements in the block that makes up the loop body, they can be separated by semicolons (;)

Example:

while n > 0: n -= 1; print(n)

Output:

4
3
2
1
0

Infinite While Loops

As the name suggests, an infinite loop will continually execute statements in a while loop until the user stops manually, like we write a while loop that theoretically never ends.

Sound interesting. Let sees the example.

i = True

while i == True:
    print("Infinite")

Same code we can write differently as follow.

while True:
    print("Infinite")

Output:

Infinite
Infinite
Infinite
Infinite
Infinite
Infinite
Infinite
Infinite
Infinite
Infinite
Infinite
Infinite
Infinite
Infinite
Infinite
Traceback (most recent call last):
  File "/home/Python/whileloop.py", line 4, in <module>
    print("Infinite")
KeyboardInterrupt

We are printing Infinite continuously until we stop using the Crt + X key using the keyboard and are also throwing an error.

 

Was this article helpful?
Share on: