Introduction

As long as the condition specified in the while loop statement is true, a while loop statement in Python may be used to continually execute a specific statement. While loops provide the computer the ability to repeatedly go over a chunk of code.

Syntax of While Loop in Python:

while test_expression:
    body of while

The while loop condition is initially evaluated by the program. If it is, the program enters the while loop and runs its body. Otherwise, it exits. While the condition is true, it keeps running the while loop's body. When it is false, the program exits the loop and ceases to iterate over the while loop's body. Let's look at the next example to better comprehend it.

a = 1


while( a<10):
print(" loop entered", a, "times")
a = a+1
print("loop ends here")

Output:

loop entered 1 times
loop entered 2 times
loop entered 3 times
loop entered 4 times
loop entered 5 times
loop entered 6 times
loop entered 7 times
loop entered 8 times
loop entered 9 times
loop ends here

Infinite While Loop

The term "infinite while loop" describes a while loop in which the while condition is permanently true. The program enters the loop and repeatedly executes the same block of code, never coming to a conclusion. This happens when a condition never turns false. A never-ending loop may be seen in the example below:

a = 1
while a==1:
b = input("Enter Name")
print("Hi", b, ", start coding.")

The code block above will conduct an indefinite loop that repeatedly asks for our names if we run it. If we don't press "Ctrl+C," the cycle will continue.

Output:

Enter Name
Akanksha #user input
Hi Akanksha , start coding.


Enter Name
Amrit #user input
Hi Amrit , start coding.


Enter Name
Shubham #user input
Hi Shubham , start coding.


Enter Name
Traceback (most recent call last): 

#Stopped the loop by entering CTRL+C

File "", line 2, in
KeyboardInterrupt

write your code here: Coding Playground

Do While Loop

There isn't a do-while loop in Python. However, we can develop a do-while software. After the statement has been executed, it is used to check the conditions. Similar to a while loop, but it runs at least once.

i = 1 
while True
print(i) 
    i = i + 1 
    if(i > 5): 
        break  

Output:

1
2
3
4
5

write your code here: Coding Playground

While True or 1

Declaring a condition to be true without evaluating any expressions is a notion. This shows that the loop must continue to execute until it breaks. The break statements are then added inside the code block. Write true inside the conditional braces rather than declaring any Python variable, setting conditions, and then incrementing it.

weeklySalary = 0
dayOfWeek = 1
week = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
while(True):
    if(week[dayOfWeek] == "Sunday"):
        print("Holiday!!")
        break
    weeklySalary += 2000
    dayOfWeek += 1
print(str(weeklySalary))

Output:

Holiday!!
10000

While Loop with Else

The else statement can be used with loops in Python. When the while loop is used, the else sentence is only used if the condition changes to false.

a = 1

while a<5:
print("condition is true")
a=a+1

else:
print("condition is false now")

The example illustrates how the else statement works with the while loop.

Output:
condition is true
condition is true
condition is true
condition is true
condition is false now

In the aforementioned example, the program continues to run the while loop's body until the condition is satisfied, which means that the value of an is less than 5. The condition is false when the program enters the loop for the fourth time when the value of an is increased from 4 to 5, as the initial value of an is 1 and its value is increased by 1 each time the program enters the loop. The program runs the condition as false after the fifth time it is checked, moves to the else block, and then executes the else block's body, indicating that the condition is now false.

Conditional Interruptions

The following two keywords are available in Python and can be used to end a loop iteration early.

  1. Break: The break keyword breaks the loop and moves control to its conclusion.


a = 1
while a <5:
a += 1
if a == 3:
break
print(a)

Output:
2

2. Continue: The continue keyword ends the current iteration, moves control to the top of the loop, and performs a new evaluation of the loop condition. The following repetition begins if the condition is satisfied.

Example:

a = 1
while a <5:
a += 1
if a == 3:
continue
print(a)

Output:
2
4
5

Factorial with While Loop

num = int(input("Enter a number: "))
fac = 1
i = 1
while i <= num:
    fac = fac * i
    i = i + 1
print("Factorial of ", num, " is ", fac)

The output will be
Enter a number: 4
Factorial of  4  is  24

write your code here: Coding Playground