For loop in C

05 min read

Sometimes, the same set of statements has to be executed repeatedly, to arrive at a solution. For example, to calculate the total marks of all students in a class of 65 students and hence their grade, the same block of statements have to be executed 65 times. This is called as looping. There are three looping constructs in C.

1) for(......)               // if the number of repetitions is known well in advance.

2) while(.....)          //if the number of repetitions cannot be predicted and hence the repetitions must be carried out till some condition is satisfied.

3) do-while(....)   //if the number of repetitions cannot be predicted and hence the repetitions must be carried out till some condition is satisfied.

 

1) for-loop: This is an entry-controlled loop.

Syntax:

for (expression1; expression2; expression3)
{
//body of'the loop
}
statement-x;

  • If a single statement has to be executed, need not include the flower braces ' {' and ' }'.
  • Expression 1 Initialization parameter which is usually an assignment statement.
  • Expression2 — Test condition which must be true to continue with the loop execution which is usually written as a logical testing statement.
  • Expression3 — Reevaluation parameter, which alters the value of initialization parameter for every loop repetition or iteration which may be a unary expression or assignment expression.
  • Expression l is executed only once while entering the loop.for the very first time
  • Expression2 is tested at the beginning of each loop iteration.
  • Expression3 is executed at the end of each iteration. 

Example:  Program to print the numbers from 1 to 5. 

int main( )
{
int i;
printf("The numbers are \n");
for(i= 1; i<=5; i++)
{
printf("%d\t",i);
}
getch( );
return 0;
}

 

Output

1     2    3   4   5

 

2) Nested for loops: In these statements, for every value of outer loop's iteration variable, the inner loop is executed.

Syntax

for (initialization; test_condition; increment/decrement)
{
for (initialization; test condition; increment/decrement)
{
//body of the loop
}
}
statement-x;

Example: To print Multiplication Table

int main( )
{
int i, j;
printf("Multiplication Table: \n");
for(i=1;i<=10;i++)
{
for(j=1 ;j<=1 0;j++)
{
printf("%d*%d=%d \t",j, i, i*j);
}
printf("\n");
getch( );
return 0;
}

 

Output:

1*1=1          2*1=2          3*1=3   ..........  10*1=10
...............

1*10=10    2*10=20    3*10=30  ....... 10*10=100

 

3) While loop:  This is an entry-controlled loop. Best if the numbers of iterations are not known in advance.

Syntax

while (test_condition)
{
Statements;
}

 

  • Loop-continuation condition is tested at the beginning of the loop before the body of the loop is performed.
  • Statements are executed repeatedly as long as the condition is true.

Example: Using while loop, write a program to print the natural numbers from 1 to 10. 

int i;
i=1;    // the iteration variable should be initialized before entering the loop
while(i<= I 0)
{
printf("%d\n",i);
i++;        // increment should be done within the loop
}

So, when 'i' becomes 11, (11<=10) becomes false. Hence the loop gets terminated.

 

4) do-while loop: This looping is best if the numbers of iterations are not known in advance.

Syntax

do
{
statements
} while(condition);

 

  • The loop-continuation condition is tested after the loop body is performed.
  • The loop is executed at least once.
  • Statements within the loop are executed repeatedly as long as the condition is true.

Example: Printing natural numbers from 1 to 5 using do-while loop.

int i; 
i=1;
do
{
printf("%d\t",i);
i++;
} while(i<=10);

 

Output

1         2         3         4        5
POST A NEW COMMENT
     
  • Input (stdin)

    Output (stdout)


    Input (stdin)

    Your Output (stdout)

    Expected Output

    Compiler Message

    Input (stdin)

    2    3

    Your Output (stdout)

    5

    Expected Output

    5

    Compiler Message

    5

    Error