C Programming For Loop

Last modified by Microchip on 2025/08/26 05:25

A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.

Syntax

for (expression1; expression2; expression3)
statement

expression1 initializes a loop count variable once at the start of the loop (e.g. i = 0). expression2 is the test condition – the loop will continue while this is true (e.g. i <= 10). expression3 is executed at the end of each iteration – usually to modify the loop count variable (e.g. i++) 

For loop schematic

1 int i;
2
3 for (i = 0; i < 5; i++)
4 {
5         printf("Loop iteration %d\n", i);
6 }

The expected output for this for loop is:

Loop iteration 0
Loop iteration 1
Loop iteration 2
Loop iteration 3
Loop iteration 4

It is important to mention that any or all of the three expressions in a for loop may be left blank (semi-colons must remain). If expression1 or expression3 are missing, their actions simply disappear. If expression2 is missing, it is assumed to always be true.

Information

Infinite Loops
A for loop without any expressions will execute indefinitely (can leave loop via break statement)
Infinite loops