For loop in C Language

When we want to execute a set of statements or a block of code repeatedly till a particular condition is true, we use LOOP. There exists 3 basic loops –

  • For loop
  • While loop
  • do while loop
void main(){
int x;

   printf("value of x: %d\n",x);

To use for loop we need to start with a keyword for then in round bracket we mention initialization ; condition ; increment/decrement semicolon separated. the in the curly bracket we mention the set of statements or block of code that we need to execute repeatedly till the condition is true. we use increment/decrement to reach closer to the termination condition.

It all starts with initialization (x=1), then condition is checked (x<10) if true execute the for loop body. Lets say the condition was true and the for loop body got executed, then the control goes to increment(x++), the condition(x<10) is checked, if true execute the for loop body. This will continue till x=9, but when x is incremented as x=10, then the condition returns false and the control of program says ‘BYE’ to for loop and starts executing the rest of the code.

Leave a Reply

Your email address will not be published. Required fields are marked *