Pages

10.for Loop Statement



for (initialization_expression;loop_condition;increment_expression){
  // statements
}
There are three parts that are separated by semi-colons in control block of the C for loop.
  • initialization_expression is executed before execution of the loop starts. This is typically used to initialize a counter for the number of loop iterations. You can initialize a counter for the loop in this part.

  • The execution of the loop continues until the loop_condition is false. This expression is checked at the beginning of each loop iteration.

  • The increment_expression, is usually used to increase (or decrease) the loop counter. This part is executed at the end of each loop iteration.
Here is an example of using C for loop statement to print an integer from 0 to 4 on screen:
#include <stdio.h>
 
void main(){
    // using for loop statement
    int max = 5;
    int i;
    for(i = 0; i < max;i++){
 
        printf("%d\n",i);
 
    }
}
And the output is
0
1
2
3
4

If you like this please Link Back to this article...



0 comments:

Post a Comment