×


Python While Loop in Linux

while loop in python is used to execute a particular piece of code over and over. It will repeat until the condition you set is satisfied. The line immediately after the loop will be executed if the condition becomes false. Basically, while loop is of indefinite iteration.

Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform related Python function queries.

In this context, we shall look into how to use the while loop in Python.


What is the While Loop in python ?

The while loop will loop until the condition you set is satisfied.

If the condition becomes false, the line after loop will be executed.

The Syntax of while Loop in Python is given below:

while test_expression:
    Body of while

In the while loop, test expression is checked first. The body of the loop is entered only if the test_expression evaluates to True. After one iteration, the test expression is checked again. This process continues until the test_expression evaluates to False.

In Python, the body of the while loop is determined through indentation.

The body starts with indentation and the first unindented line marks the end.

Python interprets any non-zero value as True. None and 0 are interpreted as False


Examples of using Python while Loop

1. Let's take a quick look at the below While Loop function:

# Program to add natural
# numbers up to 
# sum = 1+2+3+...+n
# To take input from the user,
# n = int(input("Enter n: "))
n = 10
# initialize sum and counter
sum = 0
i = 1
while i <= n:
    sum = sum + i
    i = i+1    # update counter
# print the sum
print("The sum is", sum)

When you run the program, the output will be:

Enter n: 10
The sum is 55

Here, the test expression will be True as long as our counter variable i is less than or equal to n (10 in our program).

We need to increase the value of the counter variable in the body of the loop. This is very important (and mostly forgotten). Failing to do so will result in an infinite loop (never-ending loop).


2. While loop with else

Same as with for loops, while loops can also have an optional else block.

The while loop can be terminated with a break statement. In such cases, the else part is ignored. Hence, a while loop's else part runs if no break occurs and the condition is false.

For example,

'''Example to illustrate
the use of else statement
with the while loop'''
counter = 0
while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")

If you run the program, the Output will be:

Inside loop
Inside loop
Inside loop
Inside else

Here, we use a counter variable to print the string Inside loop three times.

notice that on the fourth iteration, the condition in while becomes False. Hence, the else part is executed.


3. Consider the below function:

i = 2
while i <= 8:
print(i)
i += 2

The output will be:

2
4
6
8


4. while loop with break statement

break still stops the loop even though the condition is true.

For example,

Stop the loop when i = 6:

i = 2
while i <= 8:
print(i)
if i == 6:
break
i += 2

The output will be:

2
4
6


5. Sum the numbers:

print("Enter the ending number: ")
n = int(input())
sum = 0
i = 1
while i <= n:
sum += i
i += 1
print("Sum =",sum)

The output will be:

Enter the ending number: 3
Sum = 6


[Need help in fixing Python function issues? We can help you. ]


Conclusion

This article covers how to use the while loop in Python. In fact, Loops are used in programming to repeat a specific block of code.