C

for loop

for loop is used to execute a set of statements repeatedly until a particular condition is satisfied. we can say it an open ended loop.
General format is,

Syntax:

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

In for loop we have exactly two semicolons, one after initialization and second after condition.
In this loop we can have more than one initialization or increment/decrement, separated using comma operator. for loop can have only one condition.

Example:

//Write a Program to Print First 10 Natural Numbers using for loop.
#include<stdio.h>
#include<conio.h>
void main()
{
    int i;
    for(i=1; i<=10; i++)
    {
        printf("%d ",i);
    }
    getch();
}

Output:

Command Prompt
1 2 3 4 5 6 7 8 9 10



Subscribe us on Youtube

Share This Page on


Ask Question