Description: For loop C
- Update statement (Increment or Decrement)
- OPERATOR IN THE C PROGRAMMING LANGUAGE
- IF STATEMENT IN C
- IF-ELSE STATEMENT IN C
- NESTED IF-ELSE & SWITCH STATEMENT IN C
- WHAT ARE THE LOOPS IN C
FLOW DIAGRAM OF FOR LOOP C
|Flow Diagram of for loop C|
Control flow of for loop C
- First, initialize the variable 'i=1', It executes only once when we are entering into the loop.
- In the second step check condition i.e. 'i<=5'.
- In the third step, control goes inside the loop body and execute (print Hello five times).
- Next step is to increase the value of the variable i.e. (i=i+1 i.e i=2).
- Repeat above four steps until the condition becomes false.
Common pitfalls in using for loop C
- A valid expression should be written in for loop statement otherwise it can lead to an infinite loop. The statement,
- Many variables can be initialized in the initialization block of for loop, whether you use it in the loop or not.
//Program to illustrate initializing many variables in the initialization block.
- The variables that are declared in the initialization block can be accessed only within the loop.
//Program to illustrate scope of initializing variables within the loop
int main ( )
for (int x = 3; x < 7; x++)
printf (" x=%d\n", x);
printf ( "x=%d\n", x );
error: 'x' undeclared