Control Flow

For Loop in C

The for loop is a control structure that executes a block of code a specified number of times.

int controllingVariable;

for(initialization; booleanExpression; update) {
} //end for loop


Initialization declares the initial value of the controlling variable. The controlling variable must be declared previously (if using C99 or above, declaration in line is allowed). Typically, the for loop uses an integer variable.

booleanExpression is the controlling statement that must be true in order for the 'for' structure to loop and execute. booleanExpression must result in either a true or false output. It is created using comparing operators (==, >, =, <=, !=). ; There can also be multiple boolean expressions within the parentheses (booleanExpression). The boolean expressions are connected through logical operators (&&, ||, !).

Update is the change in the controlling variable that occurs after each execution of the statements. It performs an arithmetic operation on the controlling variable.

int x; //controlling variable declaration
for(x = 0; x <10; x++) { 
     printf(" %d", x); 
} //x will continue to increment while it remains less than 10.

See Also

GNU C Manual - for Statement

Add to Slack

< Switch Case   |   While Loop >

© 2019 SyntaxDB. All Rights Reserved.