Home » Blog » Basic for Loop In C

Basic for Loop In C

for loop consists of three parts in a sequence.

Initialization: Use to initialize the loop variable.
Condition: It is checked after each iteration as an entry point to the loop.
Increment or Decrement: Incrementing or Decrementing the loop variable to eventually terminate the loop not satisfying the loop condition.
Remember that the loop condition checks the conditional statement before it loops again.

Syntax:

for(initialization, condition, incrementation or decrementation)
{ 
    code statements;
}

Example is give below

#include<stdio.h>
#include<conio.h>

void main()
{ 
    //Always declare the variables before using them 
    
    int i = 0;  // declaration and initialization at the same time

    clrscr();

    for(i = 0; i < 10; i++)
    {
        printf("i = %d\n", i);

        /*
            when i equals 10, the loop breaks.
            i is updated before the condition is checked-
            hence the value of i after exiting the loop is 10 
        */
     }

    getch();
}

OutPut

i = 0
i = 1
i = 2
i = 3
i = 4
i = 5
i = 6
i = 7
i = 8
i = 9

 

Leave a Reply

Your email address will not be published.