C Loops: For, While, Do While, Looping Statements with Example

Loop:

The versatility of the computer lies in its ability to perform a set of instructions repetedly. This involves repeating some portion of the program either a specified number of times or until aparticular condition is being satisfied. This repetitive operation is done through a loop control instruction.

There are three methods by way of which we can repeat a part of a program. They are:

  1. Using a for statement
  2. Using a while statement
  3. Using a do-while statement

The while Loop

It is often the case in programming that you want to do something a fixed number of time. Let us look at a simple example, while uses a while loop.

/*Calculation of simple interest for 3 sets of p, n and r*/
#include<stdio.h>	
int main()
{
    int p, n, count;
    float r, si;
    count = 1;
    while(count<=3)
    {
        printf("\nEnter values of p, n and r");
        scanf("%d%d%f",&p,&n,&r);
        si = p*n*r/100;
        printf("Simple interest = Rs.%f\n",si);
        count = count+1;
    }
    return 0;
}

And here are the sample runs of the program..

Enter values of p, n and r 2000 5 13.5
Simple interest=Rs. 1350.000000

The program executes all statements after the while 3 times. The logic for calculating the simple interest is written within a pair of braces immediately after the while keyword. These statements form what is called the ‘body’ of the while loop. The parentheses after the while contain a condition. So long as this condition remains true all statements within the body of the while loop keep getting executed repeatedly. To begin with, the variable count is initialized to 1 and every time the simple interest logic is executed, the value of count is incremented by one. The variable count is many a time called either a ‘loop counter’ or an ‘index variable’.

The operation of the while loop is illustrated in the following figure.

The for Loop:

Perhaps one reason why few programmers use while is that they are too busy using the for, which is probably the most popular looping instruction. The for allows us to specify three things about a loop in a single line:

  1.  Setting a loop counter to an initial value
  2.  Testing the loop counter to determine whether its value has reached the number of repetitions desired.
  3.  Increasing the value of loop counter each time the program segment within the loop has been executed.

The general form of for statement is as under:

for (initialise counter; test counter; increment counter)
{
	do this;
	and this;
	and this;
}

Let us write down the simple interest program using for. Compare this program with the one, which we wrote using while. The flowchart is also given below for a better understanding.

/*Calculation of simple interest for 3 sets of p, n and r*/
#include<stdio.h>
int main()
{
        int p, n, count;
        float r, si;
        for (coun t= 1; count <= 3; count = count + 1)
        {
                   printf(“Enter values of p, n, and r”);
                   scanf(“%d%d%f”,&p, &n, &r);
                   si = p*n*r/100;
                   printf(“Simple Interest = Rs.%f\n”,si);
       }
       return 0;
}

If you compare this program with the one written using while, you can observe that the three steps-  initialization, testing and instrumentation-  required for the loop construct have now been incorporated in the for statement.

The do-while Loop:

The do-while loop looks like this:

 do
{
       this;
       and this;
       and this;
} while (this condition is true);

There is a minor difference between the working of while and do-while loops. This difference is the place where the condition is tested. The while tests the condition before executing any of the statements within the while loop. As against this, the do-while tests the condition after having executed the statements within the loop.

This means that do-while would execute its statements at least once, even if the condition fails for the first time. The while, on the other hand will not execute its statements if the condition fails for the first time. This difference is brought about more clearly by the following program.

#includde<stdio.h>
int main()
{
      while (4<1)
              printf(“Hello there\n”);
      return 0;
}

Here, since the condition fails the first time itself, the printf() willnot getexecuted at all. Let’s now write the same program using a do-while loop.

#include<stdio.h>
int main();
{
        do
        {
                printf(“Hello there\n”);
        } while(4<1);
        return 0;
}

In this program, the printf() would be executed once, since first the body of the loop is executed and then the condition is tested.

Leave a Comment