Lesson – While Loops & Loop Control in Python
Objective
By the end of this lesson, students will be able to:
-
Understand how
whileloops work. -
Use
breakandcontinueto control loops. -
Write programs that loop until a condition is met.
-
Avoid infinite loops.
1. What is a While Loop?
A while loop repeats a block of code as long as a condition is True.
This is different from a for loop (which loops over a sequence or a set number of times).
Syntax:
while <condition>:
<code to run>
-
The
<condition>is checked before each iteration. -
If the condition is
True, the loop runs. -
If the condition is
False, the loop stops.
Example – Counting Down
count = 5
while count > 0:
print("Counting down:", count)
count -= 1 # decreases count
print("Blast off!")
Output:
Counting down: 5
Counting down: 4
Counting down: 3
Counting down: 2
Counting down: 1
Blast off!
Key points:
-
The loop stops when
count > 0becomes False. -
If we forget
count -= 1, the loop never ends (infinite loop!).
2. Loop Control Statements
Python gives us two important tools to control loops:
break – Exit the loop completely
while True:
text = input("Type 'quit' to exit: ")
if text == "quit":
print("Goodbye!")
break
print("You typed:", text)
-
breakjumps out of the loop, no more iterations.
continue – Skip the rest of the current loop iteration
n = 0
while n < 10:
n += 1
if n % 2 == 0: # if even, skip
continue
print(n)
Output:
1
3
5
7
9
-
Even numbers are skipped.
-
The loop still continues for other numbers.
3. Combining while, break, and continue
Example – Stop when total > 100
total = 0
num = 1
while True:
total += num
if total > 100:
print("Reached above 100. Stopping.")
break
num += 1
print("Final total:", total)
Example – Input validation
while True:
val = int(input("Enter an odd number: "))
if val % 2 == 0:
print("That's even! Try again.")
continue
break
print("You entered an odd number:", val)
-
Keeps asking until a valid odd number is entered.
4. Common Mistakes to Avoid
❌ Forgetting to update loop variables → infinite loop.
❌ Using while True without a break.
❌ Writing conditions that can never be False.
5. Mini-Exercises
1- Basic While Loop
Write a loop to print numbers 1 to 5. (Hint: You can do this similarly to the for loop examples, just make sure to increment your counter or you’ll loop infinitely.)
2- Break Example
Modify it to stop if the number is 3. (so it would only print 1, 2 and then stop).
3-Number Guessing Game
-
Create a number-guessing mini-game: the program has a secret number (say 7). Use a while loop to repeatedly ask the user to guess the number. If they guess correctly, break and congratulate them. If they guess too high or too low, print a hint and continue. Use input() to get user guesses inside the loop. (Be sure to convert input to int since it input() returns a string.)
6. How to Run the Code
-
Open your Python file (example:
lesson_while.py) in your IDE. -
Copy and paste one of the examples.
-
Save the file.
-
Run:
python lesson_while.py -
Watch the output and modify the code to test different conditions.
