1 / 18

πŸ”„ Session 10: Loops

The Power of Repetition

Make the computer do the boring workβ€”again and again!

Print 1 to 1000? Sum a list? Draw patterns? One loop does it all.

✨ Write once β†’ Run many times ✨

Today's Loop Mission

πŸ”„

for Loop
Count-based repetition

⏳

while Loop
Condition-based repetition

πŸ”

do-while Loop
Execute at least once

πŸ›‘

break & continue
Control loop flow

πŸ“

Nested Loops
Loops inside loops

🎯

Real Programs
Patterns, sums, tables

πŸ€” Why Loops? Without vs With

❌ Without loop (print 1 to 100)
printf("%d\n",1); printf("%d\n",2); printf("%d\n",3); // ... 97 more lines! 😫

100 lines of the same thing!

βœ… With loop
for(i=1; i<=100; i++) printf("%d\n", i);

3 lines. Done. πŸŽ‰

Less code β€’ Easy to change β€’ No copy-paste mistakes

πŸ”„ How a Loop Runs

Follow the cycleβ€”it keeps going until the condition is false!

1. Init (i=1) β†’ 2. Condition? β†’ 3. Run body β†’ 4. Update (i++) ↻ (back to 2)

When condition is false β†’ exit loop

πŸ”„ The for Loop

Best when you know how many times to repeat!

// Syntax: for(init; condition; update) for (int i = 1; i <= 5; i++) { printf("%d\n", i); } // Output: 1 2 3 4 5

1️⃣ Init

Start: i = 1

2️⃣ Condition

Continue while i <= 5

3️⃣ Update

After each run: i++

πŸ“ for Loop: Print 1 to 10

#include <stdio.h> int main() { int i; for (i = 1; i <= 10; i++) { printf("%d ", i); } printf("\n"); return 0; } // Output: 1 2 3 4 5 6 7 8 9 10

βž• for Loop: Sum of 1 to N

int n, sum = 0; printf("Enter n: "); scanf("%d", &n); for (int i = 1; i <= n; i++) { sum = sum + i; } printf("Sum = %d\n", sum);

Example: n = 5 β†’ Sum = 1+2+3+4+5 = 15

⏳ The while Loop

Repeat while a condition is true. Check condition first!

// Syntax: while(condition) { ... } int count = 1; while (count <= 5) { printf("%d ", count); count++; } // Output: 1 2 3 4 5

⚠️ Update Variable

Must change condition or infinite loop!

πŸ” The do-while Loop

Execute at least once, then check condition!

// Syntax: do { ... } while(condition); int x = 1; do { printf("%d ", x); x++; } while (x <= 5); // Output: 1 2 3 4 5

Difference: do-while runs body first, then checks. while checks first.

πŸ”„ When to Use Which?

πŸ”„ for loop

Use when: You know how many times!

β€’ Counting 1 to N, tables

β€’ Most common for repetition

⏳ while loop

Use when: "Repeat until..."

β€’ User quits, condition becomes false

β€’ Condition checked first

πŸ” do-while loop

Use when: Run at least once!

β€’ Menus: show then ask again

β€’ Condition checked last

πŸ›‘ break and continue

// break: exit the loop immediately for (i = 1; i <= 10; i++) { if (i == 5) break; printf("%d ", i); } // Output: 1 2 3 4 // continue: skip to next iteration for (i = 1; i <= 5; i++) { if (i == 3) continue; printf("%d ", i); } // Output: 1 2 4 5 (skips 3)

πŸ“ Nested Loops

A loop inside another loop! Great for patterns and tables.

// Print 3 rows, 4 stars each for (int row = 1; row <= 3; row++) { for (int col = 1; col <= 4; col++) { printf("*"); } printf("\n"); }

Output:

****
****
****

πŸ“Š Multiplication Table Example

int num = 5; for (int i = 1; i <= 10; i++) { printf("%d x %d = %d\n", num, i, num * i); } // 5 x 1 = 5, 5 x 2 = 10, ... 5 x 10 = 50

🚨 Loop Traps

❌ Infinite Loop

Forgetting to update loop variable in while

❌ Off-by-One

i <= 10 vs i < 10 β€” count carefully!

❌ Semicolon

for(i=1; i<=5; i++); ← wrong! Loop does nothing

🧠 Loop Quiz!

for loop is best when we know the number of repetitions

do-while checks condition before running the body

break exits the loop immediately

continue restarts the program from the beginning

πŸ† Loop Mastery Unlocked!

You've earned these badges:

πŸ”„ for Loop Pro
⏳ while Wizard
πŸ” do-while Expert
πŸ“ Pattern Builder
πŸ›‘ break & continue

You can make programs repeat anythingβ€”like a pro! πŸš€

🏠 Your Coding Mission!

πŸ”’ Print 1 to N

Use for loop to print numbers

βž• Sum & Product

Sum and product of 1 to N

πŸ“Š Table

Multiplication table for any number

⭐ Pattern

Print triangle or rectangle with *

πŸš€ What's Coming Next!

Arrays: Storing Many Values

Store 100 numbers in one variable? Yes, with arrays!

πŸ€” Think About This Week:

How would you store marks of 50 students?