For Loop In C: C Tutorial In Hindi #15

For Loop :


for loops are mostly or frequently used loops in C language because of their efficiency. This loop provides many more features than other type of loops.


  • The for loop is used to iterate the statements or a part of the program several times.
  • It is used to traverse the data structures like the arrays and linked lists.
  • It has a little different syntax than while and do-while loops.


In for loops first of all the condition is checked and if it is true then statements of for loop runs otherwise it terminates. But if condition is true then after checking condition it execute statements and after executing statements it checks the iteration of variable and increases/decreases its value and then again follows the same procedure i.e. checking condition and all.


Syntax of for loop :

for (expression 1; expression 2; expression 3)

{

Statement 1;

// code inside for block

}


Here, Expression 1 – Initialization

          Expression 2 – Condition

          Expression 3 – Iteration (increment/decrement)


Properties of expression 1 :

  • It represents the initialization of the loop variable.
  • We can initialize more than one variable in expression 1.
  • Expression 1 is optional.


Properties of expression 2 :

  • It is conditional expression. It checks for a specific condition to be satisfied. If it is not, the loop is terminated.
  • It can have more than one condition. However the loop will iterate until the last condition becomes false. Other conditions will be treated as statements.
  • It is optional.
  • It can perform the task of expression 1 and expression 3. i.e. we can initialize and update the loop variable.
  • We can pass zero or non-zero value in expression 2. But non-zero means ‘true’ and zero means ‘false’.


Properties of expression 3 :

  • It is used to update the loop variable.
  • We can update more than one variable at same time.
  • It is optional.


So, for loops are used in program to iterate any block of code again and again and mainly loops are used to execute any code many times. for loop is one of the widely used loop in c language as it allows us to enter and manipulate data in Arrays and other data structures.





So, that’s all about for loop in C Language.

Code as described/written in the video

#include <stdio.h>

int main()
{
    printf("Hello World\n");
    int i, j=0;
    for(i=0 ;  ; )
    {
       printf("%d %d\n", i, j);
       i++;j++;
    }
    
    return 0;
}



Be the first person to comment!

Comments(0)