Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

Master C Loops: For, While, and Do‑While Explained with Practical Examples

What Is a Loop in C?

A loop repeats a block of statements until a specified condition becomes false. In C, a loop consists of a body and a control statement that dictates how many times the body executes. Mastering loops is essential for writing efficient and readable C programs.

In this tutorial you’ll discover:

Types of Loops in C

C loops are classified by the position of their control condition:

An infinite loop occurs when the loop never satisfies its termination condition. Common causes include missing a termination check or an always‑true condition.

While Loop in C

The while loop is the simplest form of an entry‑controlled loop. Its syntax is:

while (condition) {
    statements;
}

The condition is evaluated first; if it is true, the body executes. After each execution, the condition is re‑evaluated until it becomes false.

Example – print numbers 1 to 10:

#include <stdio.h>
int main() {
    int num = 1;
    while (num <= 10) {
        printf("%d\n", num);
        num++;
    }
    return 0;
}

Output:

1
2
3
4
5
6
7
8
9
10
Master C Loops: For, While, and Do‑While Explained with Practical Examples
While Loop in C Programming

Do‑While Loop in C

The do‑while loop guarantees that the body executes at least once, making it an exit‑controlled loop. Syntax:

do {
    statements;
} while (condition);

Example – print the multiplication table of 2 up to 10:

#include <stdio.h>
int main() {
    int num = 1;
    do {
        printf("%d\n", 2 * num);
        num++;
    } while (num <= 10);
    return 0;
}

Output:

2
4
6
8
10
12
14
16
18
20
Master C Loops: For, While, and Do‑While Explained with Practical Examples
Do‑While Loop in C Programming

For Loop in C

The for loop is highly efficient for iterating with a known start, end, and step. Syntax:

for (initialization; condition; increment) {
    statements;
}

Example – print numbers 1 to 10:

#include <stdio.h>
int main() {
    for (int number = 1; number <= 10; number++) {
        printf("%d\n", number);
    }
    return 0;
}

Output:

1
2
3
4
5
6
7
8
9
10
Master C Loops: For, While, and Do‑While Explained with Practical Examples
For Loop in C Programming

Complex for loops can include multiple expressions and even empty components:

for (int i = 0, j = num; i < j; i++, j--) {
    // statements
}

Nested loops are common, especially for multidimensional arrays. Example – multiplication tables:

#include <stdio.h>
int main() {
    int i, j;
    int table = 2;
    int max = 5;
    for (i = 1; i <= table; i++) {
        for (j = 0; j <= max; j++) {
            printf("%d x %d = %d\n", i, j, i * j);
        }
        printf("\n");
    }
    return 0;
}

Output:

1 x 0 = 0
1 x 1 = 1
1 x 2 = 2
1 x 3 = 3
1 x 4 = 4
1 x 5 = 5

2 x 0 = 0
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10

Break Statement in C

The break keyword exits the nearest enclosing loop or switch statement immediately. Example – stop a while loop when a value reaches 3:

#include <stdio.h>
int main() {
    int num = 5;
    while (num > 0) {
        if (num == 3)
            break;
        printf("%d\n", num);
        num--;
    }
    return 0;
}

Output:

5
4

Continue Statement in C

The continue keyword skips the rest of the current iteration and proceeds to the next loop cycle. Example – skip printing the number 5:

#include <stdio.h>
int main() {
    int nb = 7;
    while (nb > 0) {
        nb--;
        if (nb == 5)
            continue;
        printf("%d\n", nb);
    }
    return 0;
}

Output:

6
4
3
2
1

Choosing the Right Loop

Decide based on when the condition should be evaluated:

Summary

C Language

  1. Mastering C# While and Do‑While Loops: Syntax, Examples, and Best Practices
  2. Mastering C# For Loops: Syntax, Flow, and Practical Examples
  3. Master C++ For Loops: A Step-by-Step Guide
  4. Mastering the C for Loop: Syntax, Mechanics, and Practical Examples
  5. Mastering While and Do‑While Loops in C: Practical Examples
  6. Mastering Python's While Loop: Syntax, Examples, and Best Practices
  7. C# Control Flow: IF, SWITCH, FOR, WHILE – Practical Examples & Explanations
  8. Master Python Loops: For, While, Break, Continue, and Enumerate Explained
  9. Mastering C Loops: A Comprehensive Guide to Repetition Control
  10. Master C# Loops: Efficient Code Repetition Techniques