MASTER YOUR LOGIC BUILDING:Phase 2 - Level 5: Logical Loop Combinations
Mastering phase 2 - level 5: logical loop combinations concepts and implementation.
Phase 2 - Level 5: Logical Loop Combinations
Why This Matters
Real programs don't just repeat blindly — they repeat with rules. "Add up all donations, but skip any $0 entries." "Find every number between 1 and 100 whose digits sum to an even number." These problems combine loops with the if logic you learned in Phase 1.
This is where everything comes together — and where programming starts to feel powerful.
What You'll Learn
- How to filter items inside a loop using
if - How to accumulate results only when a condition is met
- How
continueskips one iteration without stopping the loop - How
breakexits a loop early when you're done
Three Powerful Loop Patterns
1. Filter — process only what matches
for num in range(1, 101):
if digit_sum_is_even(num):
print(num)
Loop through everything, but act only on items that pass the test.
2. Accumulate with a condition — sum only the good ones
total = 0
for num in numbers:
if num > 0:
total += num
Keep a running total, but only add when the condition is true.
3. Skip with continue — ignore specific items
for i in range(5):
num = int(input("Enter a number: "))
if num == 0:
continue # skip the rest of this iteration
total += num
continue says "skip to the next number" — the loop keeps going.
Step-by-Step: Even Digit Sum
Problem: Find numbers 1–100 whose digits add up to an even total.
- Loop through each number
- Extract digits and compute their sum (inner
whileloop) - Check if the sum is even (
sum % 2 == 0) - If yes, print the number
for num in range(1, 101):
digit_sum = 0
temp = num
while temp > 0:
digit_sum += temp % 10
temp //= 10
if digit_sum % 2 == 0:
print(num, end=" ")
Think: outer loop picks the number, inner loop processes its digits, if decides whether to keep it.
continue vs break
| Keyword | What it does | When to use |
|---|---|---|
| `continue` | Skips **this** iteration, loop continues | "Ignore zeros, process the rest" |
| `break` | Exits the **entire** loop | "Found what I need, stop searching" |
Common Mistakes
- Forgetting to reset variables —
digit_summust start at 0 inside the outer loop for each new number - Using `break` when you mean `continue` —
breakstops everything;continuejust skips one item - Not initializing the accumulator — set
total = 0before the loop - Putting the `if` outside the loop — the check must be inside, once per item
Tips
- Break the problem into two parts: "loop through items" and "check a condition"
- Name your conditions clearly:
digit_sum % 2 == 0reads better than a mystery variable - Trace with 3–4 small inputs before running on 1–100
- Combine patterns: filter + accumulate is one of the most common real-world loops
Practice Focus
Tackle the examples below:
- Find numbers whose digit sum is even — nested loop + condition
- Sum five inputs while skipping zeros — practice
continue - Try writing the plain-English rule first, then map it to filter / accumulate / skip
Hands-on Examples
Numbers with Even Digit Sum
# Find numbers 1-100 with even digit sum
print("Numbers with even digit sum:")
for num in range(1, 101):
# Calculate digit sum
digit_sum = 0
temp = num
while temp > 0:
digit_sum += temp % 10
temp //= 10
# Check if sum is even
if digit_sum % 2 == 0:
print(num, end=" ")For each number, calculate sum of digits. If sum is even, print the number. Use modulo to extract digits.
Related Page
🔗Related Content
- 💻
Phase 2 - Practice Problems
Practice Phase 2 concepts with hands-on coding problems
- 📝
Phase 2 - Quiz
Test your Phase 2 understanding with assessment questions
- ➡️
Phase 3 - Get Started
Continue to Phase 3 after mastering Phase 2
- 🎓
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- 🧠
Logic Building Overview
Learn about the complete logic building curriculum