C For Loop

C For loop statement executes a block of statements repeatedly in a loop based on a condition.

C For loop differs from While Loop in syntax. Initialization and Update are part of the syntax in for loop. Otherwise, in most of the cases, you can do the same task that a for loop does, using a while loop.

In this tutorial, we will learn the syntax of for loop, its execution flow using flow diagram, and its usage using example programs.


Syntax of C For Statement

The syntax of For loop statement in C language is given below.

</>
Copy
for (initialization; condition; update) {
  //for block statement(s)
}

where

  • for is C builtin keyword.
  • initialization part can contain variable initialization. The variables declared here have a scope of this for loop and therefore are visible only in this for loop. You can declare and initialize one or more variables of same type in initialization.
  • condition is a boolean value or an expression that evaluates to boolean value. The condition is generally formed using comparison operators and logical operators.
  • update statement can contain comma separated assignment statements or arithmetic operations like increment, decrement.
  • () parenthesis surround the initialization, condition and update.
  • //for block statement(s) is a set of statements. This block of statement(s) is also called for block, in short. You can have none, one or multiple statements inside for block.
  • {} flower braces wrap around for block statement(s). These are optional only in case that there is only one statement inside for block. Otherwise, these braces are mandatory.

Flow Diagram of C For Loop Statement

The flow of execution for a C For Loop statement is given in the following flow diagram.

C For Loop

The for statement execution starts with evaluating the condition. If the condition evaluates to true, the program executes the statement(s) inside for block, and goes to the condition again. But, if the condition evaluates to false, during any iteration, the for loop execution is completed, and the program continues with the execution of subsequent statements present after for statement.


Examples of a For Loop

1. Printing Numbers from 1 to N using For Loop

In this example, let us print numbers from 1 to 5 using a for loop.

main.c

</>
Copy
#include <stdio.h>

int main() {
    // Using for loop to print numbers from 1 to 5
    for (int i = 1; i <= 5; i++) {
        printf("%d ", i);
    }
    
    return 0;
}

Explanation:

  1. The variable i is initialized to 1 before the loop starts.
  2. The loop continues as long as i <= 5.
  3. After each iteration, i is incremented by 1 using i++.
  4. Inside the loop, printf() prints the value of i.

Output:

1 2 3 4 5

2. Using for Loop to Iterate Over an Array

In this example, we will use For loop to traverse an integer array. We start at the first element in the array, and then iterate over the indices of the elements, accessing them one by one and printing the element to output, until we reach the end of array.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int numbers[] = {10, 20, 30, 40, 50};
    int size = sizeof(numbers) / sizeof(numbers[0]);

    // Using for loop to iterate over an array
    for (int i = 0; i < size; i++) {
        printf("%d ", numbers[i]);
    }

    return 0;
}

Explanation:

  1. We declare an integer array numbers[] containing 5 elements.
  2. We calculate the size of the array using sizeof(numbers) / sizeof(numbers[0]), which determines the number of elements.
  3. We use a for loop where i starts at 0 and iterates up to size - 1.
  4. Inside the loop, we use printf() to print each array element.

Output:

10 20 30 40 50

Break C For Loop

You can break a for loop in C, using break statement. When break statement executes, the surrounding loop is deemed completed and the control comes out of the for loop.

In the following example, we try to print the numbers from 0 to 9, as in the previous example. But when the number is 5, we break the for loop.

C Program

</>
Copy
#include <stdio.h>

void main() {
    for (int i = 0; i < 10; i++) {
        if (i == 5) {
            break;
        }
        printf("%d\n", i);
    }
}

Output

0
1
2
3
4
C For Loop Break

Continue C For Loop

You may skip the execution of steps inside for block and continue with next iterations using continue statement. continue statement takes the execution to update section of for loop, which is the next logical step after for block.

C Program

</>
Copy
#include <stdio.h>

void main() {
    for (int i = 0; i < 10; i++) {
        if (i == 5) {
            continue;
        }
        printf("%d\n", i);
    }
}

Output

0
1
2
3
4
6
7
8
9
C For Loop Continue

Nested C For Loop

For Loop is just like another C statement. So, in the for block, you can have a for loop statement. This process of writing a for loop inside a for loop is called For Loop Nesting.

C Program

</>
Copy
#include <stdio.h>

void main() {
    for (int i = 0; i <= 5; i++) {
        for (int j = 0; j <= i; j++) {
            printf("%d ", j);
        }
        printf("\n");
    }
}

Output

0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
0 1 2 3 4 5
C For Loop Nesting

Conclusion

In this C Tutorial, we learned what a For Loop statement is in C programming language, how its execution happens, and how it works with example programs.