for Loop — When You Know the Count
Use a for loop when you know exactly how many times to repeat. It packs the init, condition, and update all in one line — making it the most compact and readable loop for counted repetition.
for loop anatomy
#include <stdio.h> int main() { // Print 1 to 5 for (int i = 1; i <= 5; i++) { printf("%d ", i); } printf("\n"); // Multiplication table of 7 for (int i = 1; i <= 10; i++) { printf("7 x %2d = %d\n", i, 7 * i); } // Count down for (int i = 5; i >= 1; i--) { printf("%d ", i); } printf("\n"); return 0; }
1 2 3 4 5 7 x 1 = 7 7 x 2 = 14 ... 7 x 10 = 70 5 4 3 2 1
i++ adds 1, i-- subtracts 1, i+=2 adds 2 (counts even numbers). You can also use i*=2 to double each time.
while Loop — Condition-Driven Repetition
Use a while loop when you do not know how many times to loop — only when to stop. It checks the condition before running the body. If the condition is false from the start, the body never runs.
#include <stdio.h> int main() { // Sum of digits of a number int n = 1234, sum = 0; while (n > 0) { sum += n % 10; // add last digit n /= 10; // remove last digit } printf("Sum of digits: %d\n", sum); // 10 // Keep asking until user enters 0 int input; printf("Enter numbers (0 to stop):\n"); scanf("%d", &input); while (input != 0) { printf("You entered: %d\n", input); scanf("%d", &input); } printf("Done!\n"); return 0; }
while(1) loops forever unless you use break.
do-while Loop — Runs At Least Once
The do-while loop checks the condition after running the body — so the body always runs at least once, even if the condition is immediately false. Perfect for menus that must show at least once.
#include <stdio.h> int main() { int choice; do { printf("\n=== MENU ===\n"); printf("1. Say Hello\n"); printf("2. Show Date\n"); printf("3. Exit\n"); printf("Enter choice: "); scanf("%d", &choice); switch (choice) { case 1: printf("Hello!\n"); break; case 2: printf("Today is a great day!\n"); break; case 3: printf("Goodbye!\n"); break; default: printf("Invalid choice!\n"); } } while (choice != 3); // keep looping until user exits return 0; }
| Loop | Checks Condition | Minimum Runs | Best Use Case |
|---|---|---|---|
| for | Before each run | 0 times | Known count — print table, sum 1 to n |
| while | Before each run | 0 times | Unknown count — read until 0, process digits |
| do-while | After each run | 1 time always | Menus, input validation |
break & continue — Control Loop Flow
break — exits the loop immediately. No more iterations. Execution jumps to the line after the loop.
continue — skips the rest of the current iteration only. The loop then checks its condition and continues with the next iteration.
#include <stdio.h> int main() { // break — stop loop when we find 5 printf("break example: "); for (int i = 1; i <= 10; i++) { if (i == 5) break; // exit loop at 5 printf("%d ", i); } printf("\n"); // continue — skip even numbers, print only odd printf("continue example: "); for (int i = 1; i <= 10; i++) { if (i % 2 == 0) continue; // skip even printf("%d ", i); } printf("\n"); return 0; }
break example: 1 2 3 4 continue example: 1 3 5 7 9
Nested Loops — Loop Inside a Loop
A nested loop is simply a loop placed inside another loop. The inner loop runs completely for every single iteration of the outer loop. Nested loops are used for patterns, matrices, and 2D data.
#include <stdio.h> int main() { // Star pattern — nested loop classic for (int i = 1; i <= 5; i++) { // outer: rows for (int j = 1; j <= i; j++) { // inner: columns printf("* "); } printf("\n"); } printf("\n"); // Multiplication table 1-5 x 1-5 for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) { printf("%4d", i * j); } printf("\n"); } return 0; }
* * * * * * * * * * * * * * * 1 2 3 4 5 2 4 6 8 10 3 6 9 12 15 4 8 12 16 20 5 10 15 20 25
Quick Quiz
How many times does this loop run? for(int i=0; i<5; i++)
Which loop is guaranteed to run at least once even if the condition is false?
What does continue do inside a loop?
In a nested loop where outer runs 4 times and inner runs 3 times, how many total iterations happen?
Which loop is best for reading user input repeatedly until they enter 0?
Lesson Checklist
- I understand the 3 parts of a for loop: init, condition, update
- I can write a for loop to count up and count down
- I understand while loop checks condition BEFORE running body
- I know do-while always runs body at least once
- I know when to use each type of loop
- I understand break — exits loop immediately
- I understand continue — skips current iteration only
- I can write nested loops and know inner × outer = total runs
- I completed the quiz
Day 6 Preview
- 🔧 Functions — declare, define, call Day 6
- 📥 Parameters & Return types — pass data in and out Day 6
- 🌍 Scope — local vs global variables Day 6
- ♻️ Recursion intro — functions calling themselves Day 6