CT Week 5: For Loops & Nested Loops

0. Prerequisites

NOTE

What you need to know:

  • While Loops: The basic concept of iteration.
  • Lists: Knowing that a list contains multiple items.

Quick Refresher

  • For Loop: Iterates over a sequence (like a list or range).
  • Nested Loop: A loop inside another loop.
    • Analogy: Clock. Outer loop is Hour hand, Inner loop is Minute hand.

1. Core Concepts

1.1 For Loop Syntax

  • Pseudocode:
    For each X in List L:
        Print X
    
  • Range: For i = 1 to 5 (Runs for 1, 2, 3, 4, 5).

1.2 Nested Loops (The Multiplier Effect)

  • Structure:
    For i = 1 to 3:       (Outer)
        For j = 1 to 2:   (Inner)
            Print i, j
    
  • Execution:
    • Outer starts ().
    • Inner runs completely ().
    • Outer moves ().
    • Inner runs completely again ().
  • Total Runs: Outer Count Inner Count.

2. Pattern Analysis & Goated Solutions

Pattern 1: Tracing Nested Loops

Context: “What is the final value of C?” Code:

C = 0
For i = 1 to 3:
    For j = 1 to 2:
        C = C + 1

TIP

Mental Algorithm:

  1. Identify Counts:
    • Outer (): 1, 2, 3 (3 times).
    • Inner (): 1, 2 (2 times).
  2. Multiply: Total Iterations = .
  3. Action: C increases by 1 each time.
  4. Result: .

Pattern 2: Dependent Inner Loop (Triangle Pattern)

Context: “Inner loop depends on Outer variable.” Code:

S = 0
For i = 1 to 3:
    For j = 1 to i:  <-- Depends on i
        S = S + 1

TIP

Mental Algorithm (Trace):

  1. i = 1: Inner runs 1 to 1 (1 time). S becomes 1.
  2. i = 2: Inner runs 1 to 2 (2 times). S becomes 1+2=3.
  3. i = 3: Inner runs 1 to 3 (3 times). S becomes 3+3=6. Answer: S = 6.

Pattern 3: Finding Pairs (Combinations)

Context: “Find pairs (A, B) such that A+B = 10.” Code:

List L = [2, 5, 8]
Count = 0
For each A in L:
    For each B in L:
        If A + B == 10:
            Count = Count + 1

TIP

Mental Algorithm:

  1. Grid: Compare every item with every item.
  2. Pairs:
    • (2, 2)=4, (2, 5)=7, (2, 8)=10 (Hit!)
    • (5, 2)=7, (5, 5)=10 (Hit!), (5, 8)=13
    • (8, 2)=10 (Hit!), (8, 5)=13, (8, 8)=16
  3. Total: 3 Hits.

3. Practice Exercises

  1. Basic: For i = 1 to 5. How many times?
    • Hint: 5 times.
  2. Nested: Outer 10 times, Inner 5 times. Total?
    • Hint: .
  3. Logic: For X in [1, 2, 3]: S = S + X. Final S?
    • Hint: .

🧠 Level Up: Advanced Practice

Question 1: All-Pairs Comparison

Problem: Find pair of students with same marks. Logic:

For each student A in Class:
    For each student B in Class:
        If (A.Name != B.Name) AND (A.Marks == B.Marks):
            Print "Pair found"

Efficiency: This checks each pair twice (A,B and B,A). Optimization: Inner loop should start from next student. For B from A+1 to End.

Question 2: Nested Loop Trace

Problem:

Count = 0
For i = 1 to 3:
    For j = 1 to i:
        Count = Count + 1

Trace:

  • i=1: j=1. Count=1.
  • i=2: j=1, 2. Count=1+2=3.
  • i=3: j=1, 2, 3. Count=3+3=6. Answer: 6.

Question 3: Break vs Continue

Problem: Stop processing if a “Fail” grade is found. Logic:

  • If Grade == "Fail" then Break. (Exits loop completely).
  • If Grade == "Fail" then Continue. (Skips this student, moves to next). Context: If calculating “Class Average”, use Continue (maybe exclude failures?). If checking “All Pass?”, use Break.