Day 5 Progress
0%
Day 5  ·  1 Hour

Loops — for, while, do-while

Repeat actions efficiently — the engine of every real program. Master all three loop types plus break, continue and nested loops.

0–15 min · for loop
15–30 min · while loop
30–42 min · do-while
42–52 min · break & continue
52–60 min · Nested + Quiz
1

for Loop — When You Know the Count

0 – 15 min

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

int i = 0
① Init — runs once
;
i < 5
② Condition — checked each time
;
i++
③ Update — runs after each loop
for_loop.c
C
#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;
}
terminal
output
1 2 3 4 5
7 x  1 = 7
7 x  2 = 14
...
7 x 10 = 70
5 4 3 2 1
💡 i++ vs i-- vs i+=2: The update part can be anything. i++ adds 1, i-- subtracts 1, i+=2 adds 2 (counts even numbers). You can also use i*=2 to double each time.
2

while Loop — Condition-Driven Repetition

15 – 30 min

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.

while_loop.c
C
#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;
}
⚠️ Infinite loop danger! If the condition never becomes false, the loop runs forever — program freezes. Always make sure something inside the loop moves toward making the condition false. Example: while(1) loops forever unless you use break.
3

do-while Loop — Runs At Least Once

30 – 42 min

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.

dowhile_menu.c
C
#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;
}
LoopChecks ConditionMinimum RunsBest Use Case
forBefore each run0 timesKnown count — print table, sum 1 to n
whileBefore each run0 timesUnknown count — read until 0, process digits
do-whileAfter each run1 time alwaysMenus, input validation
break, continue & nested loops
4

break & continue — Control Loop Flow

42 – 52 min

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.

break_continue.c
C
#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;
}
terminal
output
break example:    1 2 3 4
continue example: 1 3 5 7 9
5

Nested Loops — Loop Inside a Loop

52 – 58 min

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.

nested_loops.c
C
#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;
}
terminal
output
*
* *
* * *
* * * *
* * * * *

   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
💡 How to count nested loop iterations: If outer runs 5 times and inner runs 5 times, total = 5 × 5 = 25 iterations. Be careful with large nested loops — they can be very slow for big numbers.
practice & quiz
Q

Quick Quiz

58–60 min
Question 1 of 5

How many times does this loop run? for(int i=0; i<5; i++)

Question 2 of 5

Which loop is guaranteed to run at least once even if the condition is false?

Question 3 of 5

What does continue do inside a loop?

Question 4 of 5

In a nested loop where outer runs 4 times and inner runs 3 times, how many total iterations happen?

Question 5 of 5

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

Coming up next
  • 🔧 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