Types of  loop available in C 

 

“for loop”

“while loop”

“do while loop”

 

1. The for loop:

              The for loop executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.

for (Start value; continue or end condition; increase value)

           statement;

Example:

#include <stdio.h>

#include <conio.h>

void main()

{

      int i;

     clrscr();

      for(i = 0; i < 10; i++)

      {

          printf("Hello ");

          printf("World ");

      }

      getch();

}

 

2.   The while loop:

             The while loop can be used if you don’t know how many times a loop must run. Here is an example:

#include<stdio.h>

#include<conio.h>

void main()

{

    int counter, howmuch;

    clrscr();

    scanf("%d", &howmuch);

    counter = 0;

    while(counter < howmuch)

    {

          counter++;

          printf("%d ", counter);

    }

getch();

}

 

 3.   The do while loop

            The “do while loop” is almost the same as the while loop. The “do while loop” has the following form:

do

{

    do something;

}

while (expression);

 

Example:

#include<stdio.h>

#include<conio.h>

void main()

{

      int counter, howmuch;

      clrscr();

      scanf("%d", &howmuch);

      counter = 0;

      do

      {

          counter++;

          printf("%d ", counter);

      }

      while ( counter < howmuch);

      getch();

}