‘for’ statement in C
The for statement is used to execute a single statement or a block of statements repeatedly as long as the given condition is TRUE. The for statement has the following syntax and execution flow diagram…
:::::-At first, the for statement starts initialization followed by the condition evaluation. If the condition is evaluated to TRUE, the single statement or block of statements of for statement are executed.
Once the execution is being completed, the modification statement is executed and again the condition is evaluated, if the condition is TRUE, the same statements are executed again.
Exactly same process will repeat again & again till the condition is evaluated as FALSE statement.
If the evaluated condition is FALSE, the execution control moves (jumps) outside the for block.
Example Program | Write a program to display even numbers upto 10.
int n ;
printf(“All possible even numbers upto 10are:-\n”);
for( n = 0 ; n <= 10 ; n++ )
if( n%2 == 0)
printf(“%d\t”, n) ;
All possible even numbers upto 10are:-
0 2 4 6 8 10
- à THE MOST IMPORTANT POINTS TO BE REMEMBERED
- WhenEVER we use for statement, we must have to follow the following steps Respectively:::::—
- 1)for is a keyword therefore for must be used only in lower case letters.
2) Each & Every for statement must be
provided with initialization, condition and modification (They can be empty(NULL)
but it must be separated with “;”)
i.e: for ( ; ; ) or for ( ; condition ; modification ) or for ( ; condition ; )
- 3) The condition may be either a direct integer value, or a variable or a condition in for statement.
- 4)The for statement can also be an empty statement as likely do-while statement but condition is different. (So don’t be confused)