TalentJR

🐍 Python for kids · Class 6 · Lesson 15 of 18 · Free

Loop Control Patterns – Search, Skip and Stop

⏱ About 35 minutes · ▶ run real Python here — nothing to install · no sign-in

In this lesson you will

  • Search a list and stop early with break, using for-else for not found.
  • Repeat a menu with while True until the user chooses to quit.
  • Skip bad values with continue and remember results with a flag variable.

Looking for your friend's name in a list of 500 exam results? You stop the moment you find it. Waiting at a bus stop? You keep waiting until your bus comes. Programmers use a few loop patterns again and again. Today you learn the most useful ones.

What are loop control patterns in Python?

A pattern is a ready-made shape of code that solves a common problem. Cooks have recipes; programmers have patterns.

Loop patterns use break to stop a loop, continue to skip one round, and else to run code when no break happened.

Here is a quick reminder. break leaves the loop at once. continue jumps to the next round.

🎬 How Python thinks — step 1 of 32Python is about to run line 1
▶for n in range(1, 10):
2 if n % 3 == 0:
3 continue
4 if n > 7:
5 break
6 print(n)
7print("Loop over.")
📦 Variables (memory boxes)

No variables yet.

🖥️ Output
▌
✏️ Change this code and run it yourself
🐍 Python — you can edit this code
Output
1
2
4
5
7
Loop over.

💡 3 and 6 are skipped by continue. At 8, break ends the loop, so 8 and 9 never print.

How do I search a list and stop when I find it?

This is the search pattern. Loop through the items, and break when you find a match.

Stopping early saves time. If the item is first in a list of 1000, why check the other 999?

Python has a special for-else. The else part runs only if the loop finished without a break, which means not found.

🎬 How Python thinks — step 1 of 14Python is about to run line 1
▶toppers = ["Aarav", "Diya", "Kabir", "Sana"]
2wanted = input("Name to find: ")
3for name in toppers:
4 print("Checking", name)
5 if name == wanted:
6 print("Found!", wanted, "is a topper.")
7 break
8else:
9 print(wanted, "is not on the list.")
📦 Variables (memory boxes)

No variables yet.

🖥️ Output
▌
✏️ Change this code and run it yourself
🐍 Python — you can edit this code
Output
Name to find: Kabir
Checking Aarav
Checking Diya
Checking Kabir
Found! Kabir is a topper.

💡 Try the name Meera. The loop checks everyone, no break happens, and the else part runs.

How do I make a menu with while True and break?

while True: means repeat forever. It sounds dangerous, but a break inside gives the way out.

This pattern is perfect for menus and games. You do not know in advance how many rounds the user wants.

Here a canteen menu keeps taking orders until the user types done.

🐍 Python — you can edit this code
Output
Order (samosa/chai/done): samosa
Order (samosa/chai/done): chai
Order (samosa/chai/done): pizza
Not on the menu.
Order (samosa/chai/done): samosa
Order (samosa/chai/done): done
Please pay Rs 40

How do I skip bad values with continue?

Real data is often messy. Some marks may be missing or wrong, like -1 or 150.

The filter pattern uses continue to skip bad items. The rest of the loop body only sees good values.

This keeps the main work simple, because the checks happen at the top.

🎬 How Python thinks — step 1 of 26Python is about to run line 1
▶marks = [78, -1, 92, 150, 64]
2total = 0
3count = 0
4for m in marks:
5 if m < 0 or m > 100:
6 print("Skipping bad value", m)
7 continue
8 total = total + m
9 count = count + 1
10print("Average of good marks:", total / count)
📦 Variables (memory boxes)

No variables yet.

🖥️ Output
▌
✏️ Change this code and run it yourself
🐍 Python — you can edit this code
Output
Skipping bad value -1
Skipping bad value 150
Average of good marks: 78.0

What is a flag variable in a loop?

A flag is a True/False variable that remembers if something happened during the loop.

You set it to False before the loop. Inside, you change it to True when you see the thing.

After the loop, the flag tells you the answer. Here we check if any day was hotter than 40 degrees.

🐍 Python — you can edit this code
Output
Heatwave alert! Drink water.

💡 A flag does the same job as for-else. Many programmers find a flag easier to read.

How do I keep asking until the input is valid?

The retry pattern asks again and again until the answer is acceptable.

It combines while True, an if to check the answer, and break when it is good.

Here the user must pick a class from 6 to 8. Wrong answers get a helpful message and another try.

🐍 Python — you can edit this code
Output
Which class are you in (6-8)? 5
Please enter 6, 7 or 8.
Which class are you in (6-8)? 11
Please enter 6, 7 or 8.
Which class are you in (6-8)? 7
Great, Class 7 it is!

🤔 Predict the output

Think like Python: read the code and choose what it prints. You get the reason straight away.

Question 1. What does this print?

for i in range(5):
    if i == 2:
        continue
    print(i)

Question 2. What does this print?

for n in [3, 5, 7]:
    if n % 2 == 0:
        print("even found")
        break
else:
    print("all odd")

Question 3. What does this print?

count = 0
while True:
    count = count + 2
    if count > 5:
        break
print(count)

🧩 Your challenge

A shop has a list of prices. Find the first item costing more than Rs 500 and print its position number (starting from 1) and price. Skip prices of 0, which mean out of stock. If no price is above 500, print "Everything is affordable."

Expected output :
Item 5 costs Rs 799.
🐍 Python — you can edit this code

⚠️ Common mistakes (and how to fix them)

❌ Wrong

n = 1
while True:
    print(n)
    n = n + 1

No error message — the program never stops (press Stop).

✅ Right

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

while True never ends by itself. Always put a break inside, with a condition that will become True.

❌ Wrong

names = ["Ravi", "Asha", "Neel"]
for name in names:
    if name == "Asha":
        print("Found Asha")
        break
    else:
        print("Not found")

No error message – but it prints "Not found" for Ravi before finding Asha.

✅ Right

names = ["Ravi", "Asha", "Neel"]
for name in names:
    if name == "Asha":
        print("Found Asha")
        break
else:
    print("Not found")

The else was paired with the if, so it ran for every non-matching name. Move it left to pair with for.

❌ Wrong

i = 0
while i < 5:
    if i == 2:
        continue
    print(i)
    i = i + 1

No error message — the program never stops (press Stop).

✅ Right

i = 0
while i < 5:
    i = i + 1
    if i == 2:
        continue
    print(i)

continue jumped over i = i + 1, so i stayed 2 forever. In a while loop, change the counter before continue.

📌 Remember

  • Search pattern: loop, and break as soon as you find the item.
  • for-else: the else part runs only if the loop ended without break.
  • while True with break is great for menus and games.
  • Filter pattern: continue skips bad values at the top of the loop.
  • A flag variable remembers if something happened inside the loop.
  • In a while loop, update the counter before any continue.

Finished the quiz and the challenge?

Questions students ask

When does the else of a for loop run in Python?

It runs when the loop finishes all its rounds. If break stops the loop, the else is skipped.

Is while True bad practice?

No, it is common for menus and games. Just make sure a break will always be reached.

Does break stop all loops or just one?

break stops only the loop it is directly inside. An outer loop keeps going.

Should I use a flag or for-else?

Both work. A flag is easier for beginners to read; for-else is shorter once you are used to it.