Lesson Progress
0%
Loops Deep Dive  ·  while & do-while

while, do-while, break & continue

Master condition-driven loops, understand the key difference between while and do-while, and control loop flow with break and continue.

while loop
Infinite loops
do-while loop
break
continue
1

while Loop — Condition Checked First

0 – 15 min

A while loop repeats a block of code as long as a condition is true. Before every iteration, the condition is checked. If it is false from the very start, the body never runs at all.

  • Best for: when you do not know exactly how many times to repeat — only when to stop
  • Condition checked: BEFORE the body runs each time
  • Minimum runs: 0 — if condition starts false, body never executes

while loop — flowchart

Start Condition checking If true Statement If false (exit loop)
while loop — basic example
while_basic.c
C
#include <stdio.h>

int main() {
    int n = 0;

    // Runs exactly 10 times: n = 0,1,2,...,9
    while (n < 10) {
        printf("n = %d\n", n);
        n++;    // IMPORTANT: update n or loop runs forever!
    }

    printf("Loop ended. n = %d\n", n);
    return 0;
}
terminal
output
n = 0
n = 1
n = 2
...
n = 9
Loop ended. n = 10

How the while loop works — step by step:

  1. Check: Is n < 10? If yes → run body. If no → exit loop.
  2. Run body: print n, then n++ (n becomes 1)
  3. Go back to check: Is 1 < 10? Yes → run body again
  4. Repeat until n = 10: Is 10 < 10? No → exit loop
⚠️ Always update the variable inside the while loop!
If you forget n++, the condition stays true forever — infinite loop. The program freezes. Always make sure something inside the loop moves toward making the condition false.
2

Infinite while Loop — while(1)

15 – 22 min

A while(1) loop runs forever because the condition is always true (1). This is intentional in many real programs — servers, game loops, and menus use infinite loops on purpose, controlled by a break statement inside.

  • while(1) — 1 is always truthy → loops forever
  • while(0) — 0 is always false → body never runs
  • Any non-zero value in C is considered true
Infinite loop example
infinite_loop.c
C
#include <stdio.h>

int main() {
    // This loop NEVER stops on its own
    while (1) {
        printf("This loop will run forever.\n");
    }
    return 0;  // never reached!
}
Controlled infinite loop — with break
controlled_infinite.c
C
#include <stdio.h>

int main() {
    int count = 0;

    // Start with "loop forever"
    while (1) {
        count++;
        printf("Loop iteration: %d\n", count);

        // break exits when we decide to stop
        if (count == 5) {
            printf("Stopping at 5!\n");
            break;
        }
    }
    printf("After loop. count = %d\n", count);
    return 0;
}
terminal
output
Loop iteration: 1
Loop iteration: 2
Loop iteration: 3
Loop iteration: 4
Loop iteration: 5
Stopping at 5!
After loop. count = 5
💡 while(1) is used in real-world programs for:
Server programs that keep listening for connections, game loops that keep running until the player quits, embedded systems (microcontrollers) that must run forever. The break statement is how you exit them.
do-while loop
3

do-while Loop — Body Runs First

22 – 38 min

The do-while loop is the opposite of while — it runs the body first, then checks the condition. This means the body always executes at least once, even if the condition is false from the start.

  • Syntax: do { body; } while (condition); — note the semicolon at the end!
  • Condition checked: AFTER the body runs
  • Minimum runs: 1 — always runs the body once
  • Best for: menus, input validation — things that must show at least once

do-while loop — flowchart (body runs BEFORE check)

Start Statement Condition checking If true If false END
do-while — count 0 to 10
do_while.c
C
#include <stdio.h>

int main() {

    // Initialization
    int i = 0;

    do {
        // Loop body executes FIRST
        printf("%d ", i);

        // Update expression
        i++;

    } while (i <= 10);   // condition checked AFTER body ← semicolon!

    printf("\nDone!\n");
    return 0;
}
terminal
output
0 1 2 3 4 5 6 7 8 9 10
Done!
while — may never run
int n = 10;

// condition false from start
while (n < 5) {
    printf("runs\n");
}
// prints NOTHING
do-while — always runs once
int n = 10;

// body runs BEFORE check
do {
    printf("runs\n");
} while (n < 5);
// prints "runs" ONCE
Infinite do-while
infinite_dowhile.c
C
#include <stdio.h>

int main() {
    // do-while version of an infinite loop
    do {
        printf("This loop will run forever.\n");
    } while (1);   // condition 1 = always true

    return 0;
}
💡 When to use do-while vs while:
Use do-while when the action must happen at least once — menus, asking for user input, game rounds.
Use while when you want to skip entirely if the condition is already false — reading files, processing data.
⚠️ Don't forget the semicolon after while!
} while (condition); — the semicolon at the end is mandatory in do-while.
Without it you get a compiler error. This is different from a regular while loop which has no semicolon.
break & continue
4

break — Exit the Loop Immediately

38 – 48 min

The break directive halts the loop immediately — no more iterations happen. Execution jumps to the first line after the closing } of the loop. It works inside all three loop types: for, while, and do-while.

  • Exits the innermost loop only — if nested, only the inner loop breaks
  • Also used in switch statements to prevent fall-through
  • The code after the loop continues normally

break — stops loop at iteration 5

1
2
3
4
5 BREAK
6,7,8,9,10 never run
break — halt a loop early
break_demo.c
C
#include <stdio.h>

int main() {
    int n = 0;

    while (1) {   // infinite loop — but break will stop it
        n++;

        if (n == 10) {
            printf("Reached 10 — breaking!\n");
            break;   // exit the while loop immediately
        }

        printf("n = %d\n", n);
    }

    printf("After loop. n = %d\n", n);
    return 0;
}
terminal
output
n = 1
n = 2
n = 3
n = 4
n = 5
n = 6
n = 7
n = 8
n = 9
Reached 10 — breaking!
After loop. n = 10
break in for loop — find first match
break_search.c
C
#include <stdio.h>

int main() {
    int arr[] = {3, 7, 15, 2, 9, 42, 6};
    int target = 9;

    for (int i = 0; i < 7; i++) {
        printf("Checking arr[%d] = %d\n", i, arr[i]);

        if (arr[i] == target) {
            printf("Found %d at index %d!\n", target, i);
            break;   // stop — no need to check the rest
        }
    }
    return 0;
}
terminal
output
Checking arr[0] = 3
Checking arr[1] = 7
Checking arr[2] = 15
Checking arr[3] = 2
Checking arr[4] = 9
Found 9 at index 4!
← arr[5]=42 and arr[6]=6 are never checked
5

continue — Skip This Iteration Only

48 – 58 min

The continue directive skips the rest of the current iteration and jumps immediately to the next one. The loop does NOT exit — it keeps going. Only the current pass is interrupted.

  • In a while loop — jumps back to the condition check
  • In a for loop — jumps to the update step (i++), then checks condition
  • Useful for skipping unwanted values while keeping the loop running

continue — skips odd numbers, prints only even

2 ✓
4 ✓
6 ✓
8 ✓
continue — print only even numbers
continue_even.c
C
#include <stdio.h>

int main() {
    int n = 0;

    while (n < 10) {
        n++;

        // If n is odd — skip the printf below
        if (n % 2 == 1) {
            continue;   // jump back to while(n < 10) check
        }

        // Only reaches here if n is even
        printf("The number %d is even.\n", n);
    }

    return 0;
}
terminal
output
The number 2 is even.
The number 4 is even.
The number 6 is even.
The number 8 is even.
The number 10 is even.
continue in for loop — skip multiples of 3
continue_skip3.c
C
#include <stdio.h>

int main() {
    printf("Numbers 1-15 (skipping multiples of 3):\n");

    for (int i = 1; i <= 15; i++) {

        if (i % 3 == 0) {
            continue;   // skip 3, 6, 9, 12, 15 — jump to i++
        }

        printf("%d ", i);
    }
    printf("\n");
    return 0;
}
terminal
output
Numbers 1-15 (skipping multiples of 3):
1 2 4 5 7 8 10 11 13 14
💡 break vs continue — key difference:
breakexits the loop completely. No more iterations.
continueskips the current pass only. Loop continues with next iteration.
Think of it: break = quit the game. continue = skip this round, play next round.
6

Full Comparison — All Loop Types

58 – 60 min
Featureforwhiledo-while
Condition checkedBefore each runBefore each runAfter each run
Minimum executions001 always
Best forKnown count (1 to 10)Unknown count (read until 0)Menus, must run once
Infinite versionfor(;;)while(1)do{} while(1);
Semicolon after?NoNoYes — required!
break works?YesYesYes
continue works?Yes (goes to i++)Yes (goes to condition)Yes (goes to condition)
quiz
Q

Quick Quiz

Question 1 of 5

What is the minimum number of times a while loop body can execute?

Question 2 of 5

What is the key difference between while and do-while?

Question 3 of 5

What does break do inside a while loop?

Question 4 of 5

In this code — what gets printed?
while(n < 10) { n++; if(n%2==1){ continue; } printf("%d ", n); }

Question 5 of 5

Which is required in do-while that regular while does NOT need?

Lesson Checklist

  • I understand while checks condition BEFORE running body
  • I know while loop minimum runs = 0 (may never execute)
  • I understand while(1) is intentional infinite loop
  • I understand do-while checks condition AFTER running body
  • I know do-while always runs at least once
  • I remember the semicolon at end of do-while
  • I know break exits the loop completely immediately
  • I know continue skips current iteration only — loop continues
  • I can use break with while(1) to make a controlled infinite loop
  • I completed the quiz