Loops in c – While loop – For loop

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 instructions

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

  • Using a for statement
  • Using a while statement
  • Using a do-while statement

while loop

It is often the case in programming that you want to do 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 i simple example, w deally suited for such cases. Let us look at a hich uses a while loop. The flowchart shown count = 1 ; below would help you to understand the operation of the while loop

/* Calculation of simple interest for 3 sets of p, n and r */

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", si ) ;
count=count+1;
} 
} 

And here are a few sample runs…

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

while (expression)
statement

The for statement

The for allows us to specify three things about a loop in a single line:

for (expr1; expr2; expr3)
statement
is equivalent to
expr1;
while (expr2) {
statement
expr3;
}

The general form of for statement is as under:

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

/* Calculation of simple interest for 3 sets of p, n and r */

main ( )
{
int p, n, count ; float r, si ;
for ( count = 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 ) ;
}
}

Let us now examine how the for statement gets executed:

  • When the for statement is executed for the first time, the value of count is set to an initial value 1.
  • Now the condition count <= 3 is tested. Since count is 1 the condition is satisfied and the body of the loop is executed for the first time.
  • Upon reaching the closing brace of for, control is sent back to the for statement, where the value of count gets incremented by 1
  • Again the test is performed to check whether the new value of count exceeds 3.
  • If the value of count is still within the range 1 to 3, the statements within the braces of for are executed again.
  • The body of the for loop continues to get executed till count doesn’t exceed the final value 3
  • When count reaches the value 4 the control exits from the loop and is transferred to the statement (if any) immediately after the body of for

Grammatically, the three components of a for loop are expressions. Most commonly, expr1 and expr3 are assignments or function calls and expr2 is a relational expression. Any of the three parts can be omitted, although the semicolons must remain. If expr1 or expr3 is omitted, it is simply dropped from the expansion. If the test, expr2, is not present, it is taken as permanently true, so

for (;;) {

}

is an “infinite” loop, presumably to be broken by other means, such as a break or return

Nesting of Loops

The way if statements can be nested, similarly whiles and fors can also be nested. To understand how nested loops work, look at the program given below:

/* Demonstration of nested loops */ 
main( ) 
{ 
 int r, c, sum ; 
 for ( r = 1 ; r <= 3 ; r++ ) /* outer loop */ 
 { 
 for ( c = 1 ; c <= 2 ; c++ ) /* inner loop */ 
 { 
 sum = r + c ; 
 printf ( "r = %d c = %d sum = %d\n", r, c, sum ) ; 
 } 
 } 
}

Output

r = 1 c = 1 sum = 2
r = 1 c = 2 sum = 3
r = 2 c = 1 sum = 3
r = 2 c = 2 sum = 4
r = 3 c = 1 sum = 4
r = 3 c = 2 sum = 5

Loops – Do-While

the while and for loops test the termination condition at the top. By contrast, the third loop in C, the do-while, tests at the bottom after making each pass through the loop body; the body is always executed at least once

The syntax of the do is

do
statement
while (expression);

Example of do-while loop

main( ) 
{ 
 char another ; 
 int num ; 
 do 
 { 
 printf ( "Enter a number " ) ; 
 scanf ( "%d", &num ) ; 
 printf ( "square of %d is %d", num, num * num ) ; 
 printf ( "\nWant to enter another number y/n " ) ; 
 scanf ( " %c", &another ) ; 
 } while ( another == 'y' ) ; 
} 

Output

Enter a number 5
square of 5 is 25
Want to enter another number y/n y
Enter a number 7
square of 7 is 49
Want to enter another number y/n n

In this program the do-while loop would keep getting executed till the user continues to answer y. The moment he answers n, the loop terminates, since the condition ( another == ‘y’ ) fails. Note that this loop ensures that statements within it are executed at least once even if n is supplied first time itself.

Leave a Comment