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

Mastering the C for Loop: Syntax, Mechanics, and Practical Examples

Mastering the C for Loop: Syntax, Mechanics, and Practical Examples

Discover how to write, understand, and leverage the C for loop with clear explanations and hands‑on code examples.

A loop is a control structure that repeats a block of code until a specified condition becomes false. In C, there are three primary looping constructs:

  1. for loop
  2. while loop
  3. do…while loop

This tutorial focuses exclusively on the for loop. Stay tuned for our next posts covering while and do…while loops.


Understanding the for Loop

The canonical syntax, as defined by the C11 standard, is:

for (initialization; test; update) {
    /* loop body */
}

Each component plays a distinct role:

How the for Loop Works

For a deeper dive into logical conditions, review C’s relational and logical operators.


Loop Flowchart

Mastering the C for Loop: Syntax, Mechanics, and Practical Examples

Example 1: Printing Numbers 1–10

#include <stdio.h>

int main(void) {
    for (int i = 1; i <= 10; ++i) {
        printf("%d ", i);
    }
    return 0;
}

Output

1 2 3 4 5 6 7 8 9 10

Step‑by‑step breakdown:

  1. i is initialized to 1.
  2. The test i <= 10 is true, so i (1) is printed.
  3. The update ++i increments i to 2, and the loop repeats.
  4. This continues until i becomes 11, at which point the test fails and the loop ends.

Example 2: Sum of the First N Natural Numbers

#include <stdio.h>

int main(void) {
    int num, count, sum = 0;

    printf("Enter a positive integer: ");
    scanf("%d", &num);

    for (count = 1; count <= num; ++count) {
        sum += count;
    }

    printf("Sum = %d\n", sum);
    return 0;
}

Output

Enter a positive integer: 10
Sum = 55

Explanation:

  1. Variable num stores the user’s input.
  2. count starts at 1. The test count <= num is true, so sum becomes 1.
  3. After ++count, count is 2; the test remains true, adding 2 to sum (now 3).
  4. Iteration continues until count reaches 11, when the test fails and the loop stops.
  5. The final sum (55) is printed.

Next up: we’ll explore the while and do…while loops for additional control flow patterns.

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. C# foreach Loops: Iterate Arrays and Collections with Ease
  4. Master C++ For Loops: A Step-by-Step Guide
  5. Mastering Python For Loops: Syntax, Examples, and Advanced Patterns
  6. Master Java For Loops: Syntax, Examples, and Best Practices
  7. C++ For Loops Explained: Syntax, Workflow, and Practical Examples
  8. Master C Loops: For, While, and Do‑While Explained with Practical Examples
  9. Java Program to Identify Armstrong Numbers Using For Loop
  10. Mastering For Loops in Verilog: Build Reusable Hardware Logic