he programs that we have developed so far used either a sequential or a decision control instruction. In the first one, the calculations

were carried out in a fixed order; while in the second, an appropriate set of instructions were executed depending upon the outcome of the condition(s) being tested.

These programs were of limited nature, because when executed, they always performed the same series of actions, in the same way, exactly once. Almost always, if something is worth doing, it’s worth doing more than once. You can probably think of several examples of this from real life, such as eating a good dinner or going for a movie. Programming is the same; we frequently need to perform an action over and over, often with variations in the details each time. The mechanism, which meets this need, is the ‘Loop Control Instruction’, and loops are the subject of this chapter.

Loops

The versatility of the computer lies in its ability to perform a set of instructions repeatedly. This involves repeating some portion of the program either a specified number of times or until a particular 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

Each of these methods is discussed in the following pages.

The while Loop

It is often the case in programming that you want to repeat something a fixed number of times. Perhaps you want to calculate gross salaries of ten different persons, or you want to convert temperatures from Centigrade to Fahrenheit for 15 different cities. The while loop is ideally suited for this.

Let us look at a simple example that uses a while loop to calculate simple interest for 3 sets of values of principal, number of years and rate of interest. The flowchart shown in Figure 5.1 would help you to understand the operation of the while loop.


START

INPUT p, n, r

STOP

is count <= 3

Yes

No

si = p * n * r / 100

PRINT si

count = 1

count = count + 1

Figure 5.1 (Flow Chart)

Let us now write a program that implements the logic of this flowchart.

/* 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("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);
        count = count + 1;
    }

    return 0;
}

And here are a few sample runs of the program…

Enter values of p, n and r 1000 5 13.5 
Simple interest = Rs. 675.000000 

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

Enter values of p, n and r 3500 5 3.5 
Simple interest = Rs. 612.500000

The program executes all statements after the while 3 times. The logic for calculating the simple interest is written in these statements and they are enclosed within a pair of braces. These statements form the ‘body’ of the while loop. The parentheses after the while contain a condition. So long as this condition remains true the statements in 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 often called either a ‘loop counter’ or an ‘index variable’.

The operation of the while loop is illustrated in Figure 5.2.

START

STOP

test

True

False

body of loop

initialise

increment

Figure 5.2

Tips and Traps

The general form of while is as shown below.

initialize loop counter; 

while(test loop counter using a condition)
{ 
    do this; 
    and this; 
    increment loop counter;
    
}

Note the following points about while…

while(i <= 10)
while(i >= 10 && j <= 15)
while( j > 10 && ( b < 15 || c < 20 ))
while( i <= 10 )
i = i + 1 ;

is same as

while ( i <= 10 )
{ 
    i = i + 1; 
}
#include <stdio.h>

int main() {
    int i = 1;

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

    return 0;
}

This is an indefinite loop, since i remains equal to 1 forever. The correct form would be as under:

#include <stdio.h>

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

    return 0;
}
#include <stdio.h>

int main() {
    int i = 5;

    while (i >= 1) {
        printf("Make the computer literate!\n");
        i = i - 1;
    }

    return 0;
}
#include <stdio.h>

int main() {
    float a = 10.0;
    
    while (a <= 10.5) {
        printf("Raindrops on roses...");
        printf("...and whiskers on kittens\n");
        a = a + 0.1;
    }

    return 0;
}
#include <stdio.h>

int main() {
    int i = 1;

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

    return 0;
}

This is an indefinite loop, and it doesn’t give any output at all. The reason is, we have carelessly given a ; after the while. It would make the loop work like this…

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

Since the value of i is not getting incremented, the control would keep rotating within the loop, eternally. Note that enclosing printf( ) and i = i +1 within a pair of braces is not an error. In fact we can put a pair of braces around any individual statement or set of statements without affecting the execution of the program.

More Operators

There are several operators that are frequently used with while. To illustrate their usage, let us consider a problem wherein numbers from 1 to 10 are to be printed on the screen. The program for performing this task can be written using while in following different ways:

#include <stdio.h>

int main() {
    int i = 1;

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

    return 0;
}

This is the most straight-forward way of printing numbers from 1 to 10.

#include <stdio.h>

int main() {
    int i = 1;

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

    return 0;
}

Note that the increment operator ++ increments the value of i by 1, every time the statement i++ gets executed. Similarly, to reduce the value of a variable by 1, a decrement operator -- is also available.

However, never use n+++ to increment the value of n by 2, since there doesn’t exist an operator +++ in C.

#include <stdio.h>

int main() {
    int i = 1;

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

    return 0;
}

Note that += is a compound assignment operator. It increments the value of i by 1. Similarly, j = j + 10 can also be written as j += 10. Other compound assignment operators are *-=, =, / = and %=.

#include <stdio.h>

int main() {
    int i = 0;

    while (i++ < 10) {
        printf("%d\n", i);
    }

    return 0;
}

In the statement while ( i++ < 10 ), first the comparison of value of i with 10 is performed, and then the incrementation of i takes place. Since the incrementation of i happens after the comparison, here the ++ operator is called a post-incrementation operator. When the control reaches printf( ), i has already been incremented, hence i must be initialized to 0, not 1.

#include <stdio.h>

int main() {
    int i = 0;

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

    return 0;
}

In the statement while ( ++i <= 10 ), first incrementation of i takes place, then the comparison of value of i with 10 is performed. Since the incrementation of i happens before the comparison, here the ++ operator is called a pre-incrementation operator.

Summary

Exercise

[A] What will be the output of the following programs:

#include <stdio.h>

int main() {
    int i = 1;

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

    return 0;
}
#include <stdio.h>

int main() {
    int x = 4, y, z;
    y = --x;
    z = x--;
    printf("%d %d %d\n", x, y, z);
    return 0;
}
#include <stdio.h>

int main() {
    int x = 4, y = 3, z;
    z = x-- - y;
    printf("%d %d %d\n", x, y, z);
    return 0;
}
#include <stdio.h>

int main() {
    while ('a' < 'b') {
        printf("malayalam is a palindrome\n");
    }

    return 0;
}

#include <stdio.h>

int main() {
    int i;

    while (i = 10) {
        printf("%d\n", i);
        i = i + 1;
    }

    return 0;
}
#include <stdio.h> 
int main()
{ 
    float x = 1.1;
    
    while( x == 1.1 )
    {

        printf("%f\\n", x);
        x = x  0.1;
    }
    return 0;
}

[B] Attempt the following:


Classes
Quiz
Videos
References
Books