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);
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!
for (int i = 1; i <= 5; i++) {
printf("%d\n", i);
}
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;
}
β 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!
int count = 1;
while (count <= 5) {
printf("%d ", count);
count++;
}
β οΈ Update Variable
Must change condition or infinite loop!
π The do-while Loop
Execute at least once, then check condition!
int x = 1;
do {
printf("%d ", x);
x++;
} while (x <= 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
for (i = 1; i <= 10; i++) {
if (i == 5) break;
printf("%d ", i);
}
for (i = 1; i <= 5; i++) {
if (i == 3) continue;
printf("%d ", i);
}
π Nested Loops
A loop inside another loop! Great for patterns and tables.
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);
}
π¨ 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?