IG

IG Math Space

Mathematics Teaching and Research

📚 Classroom Space · Teaching Note

Control Statements

Module III — Flow Control (d) · A comprehensive classroom teaching note on break, continue and pass with flowcharts, comparison tables and example programs.

👨‍🏫 Gafoor I 🏛️ NAM College Kallikkandy 📐 Department of Mathematics 📘 Taming Python by Programming

📋 Contents

  1. Learning Objectives
  2. Introduction to Control Statements
  3. The break Statement
  4. The continue Statement
  5. The pass Statement
  6. Comparison: break vs continue vs pass
  7. Combined Examples
  8. Common Mistakes
  9. Classroom Activities
  10. Review Questions
  11. Summary
01 Learning Objectives

By the end of this class session, students will be able to:

  • Understand the purpose of control statements in loops.
  • Use break to exit a loop immediately.
  • Use continue to skip the current iteration and move to the next.
  • Use pass as a placeholder in an otherwise-empty block.
  • Differentiate clearly between break, continue and pass.
  • Apply the correct control statement in search, filtering and menu-driven programs.
  • Recognize the effect of break inside nested loops and the loop else clause.
02 Introduction to Control Statements

By default, a loop runs from start to finish, executing every iteration. But sometimes we need to alter this normal flow — stop early, skip a step, or leave a placeholder. Python provides three control statements for this purpose:

StatementPurposeEffect on Loop
breakExit the loop immediatelyLoop stops
continueSkip the rest of the current iterationLoop continues with next value
passDo nothing (placeholder)Loop continues normally

Why do we need control statements?

  • Searching: Stop as soon as the target is found — no need to keep looping.
  • Filtering: Skip values that don't meet a condition.
  • Early termination: Exit a loop when an error or invalid input is detected.
  • Placeholders: Mark code that will be filled in later, without causing an error.
💡 Note: All three statements work inside both for and while loops.
03 The break Statement

3.1 Definition

The break statement immediately terminates the loop in which it appears. Control jumps to the first statement after the loop.

3.2 Syntax

for variable in sequence:
    if condition:
        break
    statement(s)

3.3 Key Points

  • Exits the innermost loop only (in a nested loop).
  • Works with both for and while.
  • Any statements after break in the same block are skipped.
  • If the loop has an else clause, the else is skipped when break executes.

Flowchart — break

Process Item Target Found? Yes Exit Loop Immediately No Continue Loop back break → exits loop immediately Control jumps to the first statement after the loop Executes inside for or while

Fig 1. Flowchart showing break — exit the loop as soon as the target is found.

3.4 Example Programs — break

Example 1: Break out of a for loop

for i in range(1, 11):
    if i == 5:
        break
    print(i)
print("Loop ended.")
1 2 3 4 Loop ended.

Explanation: When i reaches 5, break exits the loop. Values 5 to 10 are not printed.

Example 2: Search for an element in a list

numbers = [10, 20, 30, 40, 50]
target = int(input("Enter number to search: "))

for num in numbers:
    if num == target:
        print(target, "found in the list!")
        break
else:
    print(target, "not found.")
Enter number to search: 30 30 found in the list! Enter number to search: 99 99 not found.

Example 3: break in a while loop

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

Example 4: Menu-driven program using break

while True:
    print("\n--- Menu ---")
    print("1. Say Hello")
    print("2. Say Bye")
    print("3. Exit")
    choice = input("Enter choice: ")

    if choice == "1":
        print("Hello!")
    elif choice == "2":
        print("Bye!")
    elif choice == "3":
        print("Exiting...")
        break
    else:
        print("Invalid choice.")
--- Menu --- 1. Say Hello 2. Say Bye 3. Exit Enter choice: 1 Hello! --- Menu --- 1. Say Hello 2. Say Bye 3. Exit Enter choice: 3 Exiting...

Example 5: break in a nested loop (exits only inner loop)

for i in range(1, 4):
    for j in range(1, 4):
        if j == 2:
            break
        print("i =", i, " j =", j)
i = 1 j = 1 i = 2 j = 1 i = 3 j = 1

Explanation: The break exits the inner loop only. The outer loop continues normally.

04 The continue Statement

4.1 Definition

The continue statement skips the rest of the current iteration and jumps to the next iteration of the loop. The loop does not terminate.

4.2 Syntax

for variable in sequence:
    if condition:
        continue
    statement(s)

4.3 Key Points

  • Statements after continue in the same block are skipped for the current iteration.
  • Works with both for and while.
  • In a while loop, the condition must eventually become False, otherwise you get an infinite loop.
  • The loop else clause does execute if the loop finishes normally (no break).

Flowchart — continue

Process Item Skip Item? Yes Skip Rest of Body No Continue Next iteration continue → skips current iteration Loop does NOT terminate; jumps to next value Useful for filtering and skipping unwanted values

Fig 2. Flowchart showing continue — skip the rest of the current iteration and move to the next.

4.4 Example Programs — continue

Example 6: Print only odd numbers (skip even)

for i in range(1, 11):
    if i % 2 == 0:
        continue
    print(i)
1 3 5 7 9

Example 7: Sum only positive numbers in a list

numbers = [10, -5, 20, -8, 30, -2, 40]
total = 0
for num in numbers:
    if num < 0:
        continue
    total += num
print("Sum of positive numbers:", total)
Sum of positive numbers: 100

Example 8: continue in a while loop

n = 0
while n < 10:
    n += 1
    if n == 5:
        continue
    print(n)
1 2 3 4 6 7 8 9 10

Example 9: Skip vowels in a string

word = "programming"
for ch in word:
    if ch in "aeiou":
        continue
    print(ch, end=" ")
p r g r m m n g

Example 10: Print only multiples of 3 in a range

for i in range(1, 21):
    if i % 3 != 0:
        continue
    print(i, end=" ")
3 6 9 12 15 18
05 The pass Statement

5.1 Definition

The pass statement is a null statement — it does nothing at runtime. It is used as a placeholder where Python syntax requires a statement but no action is needed.

5.2 Syntax

if condition:
    pass   # to be implemented later

5.3 Key Points

  • Does nothing — it is a placeholder.
  • Prevents a syntax error in an empty block.
  • Useful during development when you plan to fill in code later.
  • Can be used inside for, while, if, functions and classes.

Flowchart — pass

Process Item Placeholder Needed? Yes pass (do nothing) No Continue Loop back pass → do nothing (placeholder) Loop continues normally; no action is taken

Fig 3. Flowchart showing pass — a no-operation placeholder that lets the loop continue normally.

5.4 Example Programs — pass

Example 11: pass as a placeholder in a loop

for i in range(1, 6):
    if i == 3:
        pass    # TODO: implement special case for 3
    else:
        print(i)
1 2 4 5

Example 12: pass in an empty function

def my_function():
    pass   # to be implemented later

my_function()
print("Function called successfully.")
Function called successfully.

Example 13: pass in an empty class

class Student:
    pass   # class body to be defined later

s = Student()
print("Object created:", s)
Object created: <__main__.Student object at 0x...>

Example 14: pass inside if block

x = 10
if x > 0:
    pass   # positive number — nothing to do
else:
    print("Non-positive")

print("Program finished.")
Program finished.
06 Comparison: break vs continue vs pass
Featurebreakcontinuepass
EffectExit the loop entirelySkip rest of current iterationDo nothing
Loop continues?❌ No✅ Yes✅ Yes
Skips remaining statements?Yes (rest of loop)Yes (current iteration)No
Loop else executes?NoYesYes
Common useSearch, early exitFiltering, skippingPlaceholder, empty block
Typical codeif num == target: breakif num < 0: continuedef f(): pass
💡 Note: break and continue change the flow of the loop; pass is only a syntactic placeholder and has no effect at runtime.
07 Combined Examples

Example 15: break and continue together

for i in range(1, 11):
    if i == 8:
        break          # stop the loop at 8
    if i % 2 == 0:
        continue       # skip even numbers
    print(i)           # prints odd numbers before 8
1 3 5 7

Explanation: Even numbers are skipped (continue); when i reaches 8, break ends the loop.

Example 16: break with loop else

numbers = [10, 20, 30, 40, 50]
target = int(input("Enter target: "))

for num in numbers:
    if num == target:
        print("Found", target)
        break
else:
    print(target, "not found.")
Enter target: 30 Found 30 Enter target: 99 99 not found.

Explanation: If break executes, the else block is skipped. If the loop finishes without break, else runs.

Example 17: Password attempt with continue

correct_password = "python123"

for attempt in range(1, 4):
    password = input(f"Attempt {attempt} — Enter password: ")
    if password != correct_password:
        print("Wrong password. Try again.")
        continue
    print("Login successful!")
    break
else:
    print("Account locked. Too many failed attempts.")
Attempt 1 — Enter password: hello Wrong password. Try again. Attempt 2 — Enter password: python123 Login successful!

Example 18: Skip multiples of 3 and stop at 15

for i in range(1, 21):
    if i == 15:
        break
    if i % 3 == 0:
        continue
    print(i, end=" ")
1 2 4 5 7 8 10 11 13 14

Example 19: pass placeholder with continue

for i in range(1, 8):
    if i == 4:
        pass        # skip — no action for now
        continue
    if i == 6:
        break
    print(i)
1 2 3 5

Explanation: At i = 4, pass does nothing and continue skips the print. At i = 6, break exits the loop.

08 Common Mistakes to Avoid
MistakeCorrection
Expecting break to exit both loops in a nested loopbreak exits only the innermost loop
Using continue when you meant breakAsk: do you want to exit or skip?
Placing continue after the loop variable update in a whilePlace the update before the continue, otherwise infinite loop
Using pass expecting it to skip a linepass does nothing — use continue to skip
Empty block without passUse pass to satisfy the syntax
Assuming loop else runs after a breakLoop else is skipped when break executes
Forgetting the colon after if/while/forAlways end with :
💡 Note on while + continue: In a while loop, always update the loop variable before the continue statement. Otherwise, the condition never changes and you get an infinite loop.
09 Classroom Activities

🔍 Activity 1 — Predict the Output

What will the following code print? Work it out on paper first, then verify.

for i in range(1, 8):
    if i == 3:
        continue
    if i == 6:
        break
    print(i, end=" ")

🐛 Activity 2 — Debug the Code

What is wrong with the code below? Fix it.

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

Hint: What happens to n when it reaches 2?

✏️ Activity 3 — Write the Program

Write a Python program using a loop that:

  • Asks the user to enter numbers repeatedly.
  • Stops when the user enters 0.
  • Skips negative numbers using continue.
  • Prints the sum of positive numbers entered.

🧩 Activity 4 — Menu with Exit

Write a menu-driven program using while True and break with options:

  • 1. Print "Hello"
  • 2. Print "World"
  • 3. Exit

🧪 Activity 5 — Search with for-else

Given names = ["Alice", "Bob", "Charlie", "David"], write a program that asks the user for a name and prints whether it is found. Use the loop else clause with break.

10 Review Questions
  1. What is the purpose of control statements in loops?
  2. Differentiate between break and continue with examples.
  3. What is the difference between continue and pass?
  4. When does the else clause of a loop execute?
  5. How does break behave inside a nested loop?
  6. Why can continue cause an infinite loop in a while loop? How can you avoid it?
  7. Write a Python program to find the first even number in a list using break.
  8. Write a Python program to print only the odd numbers from 1 to 20 using continue.
  9. Write a Python program that uses pass as a placeholder for a function that is not yet implemented.
  10. Explain the output of:
    for i in range(1, 6):
        if i == 3:
            continue
        if i == 5:
            break
        print(i)
11 Summary
⏹️
breakExits the loop immediately. Use for search and early termination. Exits only the innermost loop in nested loops.
⏭️
continueSkips the rest of the current iteration and jumps to the next. Use for filtering and skipping values.
⏸️
passDoes nothing — a syntactic placeholder. Use to fill empty blocks without causing errors.
🔚
Loop elseExecutes only if the loop finishes normally (no break). Useful for search failure cases.
🧭
Choose the Right Statementbreak → exit; continue → skip; pass → placeholder.
⚠️
Watch OutIn while loops, update the loop variable before continue to avoid an infinite loop.
G

Gafoor I

Assistant Professor  |  Department of Mathematics  |  NAM College Kallikkandy

Teaching Note · Module III (d) · Control Statements in Python · Academic Year 2025–26