Learn › C Programming

Loops

Master for, while, and do-while loops along with break and continue statements.

The for Loop

The for loop is the most commonly used loop in C when the number of iterations is known in advance. It combines initialization, condition checking, and increment in a single compact statement. The loop body executes repeatedly as long as the condition remains true. You can declare the loop variable inside the for statement in C99 and later, which limits its scope to the loop body.

#include <stdio.h>

int main(void) {
    /* Print multiplication table for 7 */
    for (int i = 1; i <= 10; i++) {
        printf("7 x %2d = %2d\n", i, 7 * i);
    }

    /* Sum of first 100 integers */
    int sum = 0;
    for (int i = 1; i <= 100; i++) {
        sum += i;
    }
    printf("Sum of 1 to 100: %d\n", sum);

    return 0;
}

The while Loop

The while loop executes its body repeatedly as long as the condition evaluates to true. It checks the condition before each iteration, so if the condition is initially false the body never executes. This makes while loops ideal for situations where you do not know how many iterations are needed. Common uses include reading input until a sentinel value is encountered or processing data until a resource is exhausted.

#include <stdio.h>

int main(void) {
    /* Find the number of digits in a number */
    int number = 987654;
    int original = number;
    int digits = 0;

    while (number > 0) {
        digits++;
        number /= 10;
    }
    printf("Number %d has %d digits.\n", original, digits);

    /* Collatz sequence */
    int n = 27;
    int steps = 0;
    printf("Collatz sequence for %d: ", n);
    while (n != 1) {
        printf("%d -> ", n);
        n = (n % 2 == 0) ? n / 2 : 3 * n + 1;
        steps++;
    }
    printf("1 (took %d steps)\n", steps);

    return 0;
}

The do-while Loop

The do-while loop is similar to the while loop but guarantees that the body executes at least once because it checks the condition after each iteration. This is particularly useful for input validation where you want to prompt the user at least once and then repeat until valid input is received. The semicolon after the while condition is required and is a common source of syntax errors for beginners.

#include <stdio.h>

int main(void) {
    /* Simple menu system */
    int choice;

    do {
        printf("\n--- Menu ---\n");
        printf("1. Say Hello\n");
        printf("2. Say Goodbye\n");
        printf("3. Exit\n");
        printf("Enter choice: ");

        choice = 2; /* Simulated input for demonstration */

        switch (choice) {
            case 1: printf("Hello!\n"); break;
            case 2: printf("Goodbye!\n"); break;
            case 3: printf("Exiting...\n"); break;
            default: printf("Invalid choice.\n");
        }

        /* Break after one iteration for demonstration */
        if (choice != 3) choice = 3;
    } while (choice != 3);

    return 0;
}

Break and Continue

The break statement immediately exits the innermost loop, transferring control to the statement after the loop. The continue statement skips the rest of the current iteration and jumps to the next iteration of the loop. Both are powerful tools for controlling loop flow but should be used judiciously since excessive use can make loops harder to reason about. They are especially useful for early termination when a search result is found or for skipping invalid data.

#include <stdio.h>

int main(void) {
    /* Find the first multiple of 7 greater than 50 */
    for (int i = 1; i <= 100; i++) {
        if (i * 7 <= 50) {
            continue;
        }
        printf("First multiple of 7 > 50: %d\n", i * 7);
        break;
    }

    /* Print odd numbers, skip multiples of 3 */
    printf("Odd numbers (1-20) not divisible by 3: ");
    for (int i = 1; i <= 20; i += 2) {
        if (i % 3 == 0) {
            continue;
        }
        printf("%d ", i);
    }
    printf("\n");

    return 0;
}

← Decision Making · Functions →