Bytebook Compiler

Control Flow

Loops: while, do-while and for

Repeat work with the three C loops, choose the right one, and write the classic digit, factorial, prime and Fibonacci programs.

5 min read7 runnable examples4-question quiz

A loop repeats a block of code while a condition stays true. C has three loops. They can all do the same job; each one simply fits a different situation.

while

A while loop checks its condition before each pass. Use it when you do not know in advance how many times to repeat.

C
#include <stdio.h>

int main(void) {
    int n, sum = 0, count = 0;
    printf("Enter a number: ");
    scanf("%d", &n);
    while (n > 0) {
        sum += n % 10;      // take the last digit
        n /= 10;            // drop the last digit
        count++;
    }
    printf("Digits: %d, Sum of digits: %d\n", count, sum);
    return 0;
}
Output
Enter a number: 40213
Digits: 5, Sum of digits: 10
Sample input is includedTry it Yourself

do...while

A do-while loop checks its condition after each pass, so the body always runs at least once. That makes it perfect for menus:

C
#include <stdio.h>

int main(void) {
    int choice;
    do {
        printf("1) Say hi  2) Say bye  3) Quit : ");
        scanf("%d", &choice);
        if (choice == 1) printf("Hi!\n");
        else if (choice == 2) printf("Bye!\n");
    } while (choice != 3);
    printf("Menu closed.\n");
    return 0;
}
Output
1) Say hi  2) Say bye  3) Quit : 1
Hi!
1) Say hi  2) Say bye  3) Quit : 2
Bye!
1) Say hi  2) Say bye  3) Quit : 3
Menu closed.
Sample input is includedTry it Yourself

for

A for loop packs the three parts of counting into one line: initialise; condition; update.

C
#include <stdio.h>

int main(void) {
    for (int i = 1; i <= 5; i++) {
        printf("5 x %d = %d\n", i, 5 * i);
    }
    return 0;
}
Output
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25

The order is: initialise once → check condition → run body → update → check condition → … until the condition is false.

StartInitialise: i = 1i <= 5 ?YesRun the body, then i++NoContinue after the loop
How a for loop runs. After the body and i++, control jumps back to the condition.

Choosing a loop

Loop Checks condition Runs at least once? Best for
for before each pass no counting a known number of times
while before each pass no repeating until something happens
do-while after each pass yes menus, input validation

Classic loop programs

Factorial

C
#include <stdio.h>

int main(void) {
    int n;
    long long fact = 1;     // factorials grow fast, so use a big type
    printf("n = ");
    scanf("%d", &n);
    for (int i = 2; i <= n; i++)
        fact *= i;
    printf("%d! = %lld\n", n, fact);
    return 0;
}
Output
n = 10
10! = 3628800
Sample input is includedTry it Yourself

Prime check

A number is prime if no number from 2 to √n divides it. Checking only up to √n makes the loop much faster:

C
#include <stdio.h>

int main(void) {
    int n, isPrime = 1;
    printf("n = ");
    scanf("%d", &n);
    if (n < 2) isPrime = 0;
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) { isPrime = 0; break; }
    }
    printf("%d is %s\n", n, isPrime ? "prime" : "not prime");
    return 0;
}
Output
n = 97
97 is prime
Sample input is includedTry it Yourself

Fibonacci series

C
#include <stdio.h>

int main(void) {
    int n, a = 0, b = 1;
    printf("How many terms? ");
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        printf("%d ", a);
        int next = a + b;
        a = b;
        b = next;
    }
    printf("\n");
    return 0;
}
Output
How many terms? 10
0 1 1 2 3 5 8 13 21 34
Sample input is includedTry it Yourself

Reverse a number and check palindrome

C
#include <stdio.h>

int main(void) {
    int n, original, reversed = 0;
    printf("n = ");
    scanf("%d", &n);
    original = n;
    while (n > 0) {
        reversed = reversed * 10 + n % 10;
        n /= 10;
    }
    printf("Reversed: %d\n", reversed);
    printf("%d %s a palindrome\n", original, original == reversed ? "is" : "is not");
    return 0;
}
Output
n = 12321
Reversed: 12321
12321 is a palindrome
Sample input is includedTry it Yourself

Test yourself

4 questions · pick an answer to check it instantly

  1. 1

    How many times does this print Hi?

    C
    int i = 10;
    do { printf("Hi"); } while (i < 5);
  2. 2

    How many times does for (int i = 0; i <= 10; i += 2) run?

  3. 3

    Why does the prime check loop use i * i <= n?

  4. 4

    Which loop is best for a menu that must appear at least once?