Python- Day 21- While Loops

Nidhi Ashtikar
3 min readOct 24, 2024

--

A for loop iterates over a collection of items, executing a block of code for each item.

A while loop runs as long as a certain condition is true.

A simple while loop can be used to count numbers (e.g., from 1 to 5)

current_number = 1
while current_number <= 5:
print(current_number)
current_number += 1

>>

1
2
3
4
5

The loop continues while current_number <= 5 is true. Once the condition is false, the loop exits.

Many programs, like games, use while loops to keep running until the user or a condition stops them.

Letting User Control the Loop :

A while loop can allow a user to control when to quit a program by checking input.

prompt = "Enter 'quit' to end the program. "
message = ""
while message != 'quit':
message = input(prompt)
if message != 'quit':
print(message)

>>

Enter 'quit' to end the program :- kk
kk
Enter 'quit' to end the program :- jkkb
jkkb
Enter 'quit' to end the program :- quit
>>

The loop continues asking for input until the user types ‘quit’, at which point it stops.

Using a Flag to Control the Loop:

A flag is a variable that signals when to stop the program.

prompt = "Enter 'quit' to end the program:-  "
message = ""


active = True
while active:
message = input(prompt)
if message == 'quit':
active = False
else:
print (message)

>>

Enter 'quit' to end the program:- Hello
Hello
Enter 'quit' to end the program:- quit

The flag allows for more flexible exit conditions and is useful in complex programs.

Breaking Out of a Loop:

The break statement can be used to exit a loop immediately.

while True:
city = input("Enter city or 'quit': ")
if city == 'quit':
break
else:
print(f"I'd love to go to {city}!")

>>

Enter city or 'quit': Delhi
I'd love to go to Delhi!
Enter city or 'quit': quit

Skipping Parts of a Loop with continue:

The continue statement allows skipping the rest of the loop and moving to the next iteration.

Example (printing only odd numbers):

current_number = 0
while current_number < 10:
current_number += 1
if current_number % 2 == 0:
continue
print(current_number)

>>

1
3
5
7
9

Avoiding Infinite Loops:

Infinite loops occur when the loop’s condition never becomes false or if a break statement is never reached.

x = 1
while x <= 5:
print(x) # Missing x += 1 will cause an infinite loop

>>

1
1
1
1.....

To avoid infinite loops, always ensure a condition or break statement is in place to stop the loop.

Handling Infinite Loops:

If an infinite loop occurs, you can stop it using CTRL-C or closing the terminal window.

Testing your loops thoroughly helps prevent infinite loops by checking exit conditions.

By following these structured methods and paying close attention to loop conditions and exit strategies, you can efficiently use while loops in Python programs.

If you found this guide helpful then do click on 👏 the button.

Follow for more Learning like this 😊

If there’s a specific topic you’re curious about, feel free to drop a personal note or comment. I’m here to help you explore whatever interests you!

Thanks for spending your valuable time learning to enhance your knowledge!

--

--

Nidhi Ashtikar
Nidhi Ashtikar

Written by Nidhi Ashtikar

Experienced AWS DevOps professional with a passion for writing insightful articles.

No responses yet