for loop in C

In this lesson, we will learn about ‘for-loop’ in C programming which is used to iterate itself. For loop provides a mechanism to repeat the task until a particular condition in the expression part is true.

syntax of for loop in C

for (initialization; condition; increment / decrement) {
  statement block;
}
statement y;

For loop is also known as a definite or determinate loop. Inside the parenthesis of the for loop, we use three statements:

  • Statement 1: here initialization of the variable is done.
  • Statement 2: here condition of the variable or loop is checked.
  • Statement 3: here arithmetic operations take place like increment/decrement.

for loop in C explained

  • In the first step, the initialization of the variable takes place.
  • In the next step, the compiler checks the condition part. If it is true, then increment or decrement of the variable in the 3rd statement, and then the body of the for loop is evaluated and compiled; else, if false, then the control is given to the next line in the “for-loop.”
  • After the loop’s execution, the control is again passed to the conditional statement of the loop, and the 2nd step continues until the condition becomes false.

Example I of for-loop in C

#include <stdio.h>

int main() {

  float
  var;

  /* for loop execution */
  for (var = 10;
    var < 20;
    var =
    var +1) {
    printf("value of var: %d\n",
      var);
  }

  return 0;
}

Output

value of var: 10.00000

value of var: 11.00000

value of var: 12.00000

value of var: 13.00000

value of var: 14.00000

value of var: 15.00000

value of var: 16.00000

value of var: 17.00000

value of var: 18.00000

value of var: 19.00000

Example II of for-loop in C

#include <stdio.h>

int main() {
  int i;
  for (i = 1; i <= 3; i++) {
    printf("*\n");
  }
  return 0;
}

Output

*

*

*

Explanation- In the above program, a variable ‘i’ is initialized with a value equal to 1. The compiler will evaluate whether the condition given is true or not. If the condition is found true, then the body of “for loop” will get executed, and we will print the asterisk as output, and the value of the variable ‘i’ gets incremented by 1. The loop gets terminated once the condition becomes false.

Summary

  • In the ‘for- loop’ we can omit ‘one’ or all the expressions.
  • If we are omitting the expression, then there should be 2 semicolons (;) in the ‘for’ statement.
  • Multiple statements can be included in the third part of the expression by separating with commas.
  • Never use the float variable as the loop control variable as it can give imprecise value.
  • The Syntax of for loop is for (initialization statement; condition statement ; arithmetic operations)   { statement block; }
  • For loop executes a set of statement blocks until the condition becomes false.
Back to: Learn C Programming > Control Statements in C

Leave a Reply

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


The reCAPTCHA verification period has expired. Please reload the page.