Loop Control Instruction

Loop Control Instruction helps the computer to execute a group of statements repeatedly.

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:

  • Using a while loop
  • Using a for loop
  • Using a do-while loop

1) The while loop

The general form or syntax of while is as shown below:

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

Note the following points about while…

The statements within the while loop would keep on getting executed till the condition being tested remains true. When the condition becomes false, then control passes to the first statement that follows the body of the while loop. In place of the condition, there can be any other valid expression. So long as the expression evaluates to a non-zero value the statements within the loop would get executed. As a rule, the while must test a condition that will eventually become false, otherwise, the loop would be executed forever. Instead of incrementing a loop counter, we can even decrement.

We can understand while loop with the help of the following example:

/* 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
}
}

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

2) The for loop

The for loop in C is much more versatile than other loops. The general form of for loop is as shown below:

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

The for allows us to specify three things about a loop in a single line. Setting a loop counter to an initial value. Testing the loop counter to determine whether its value has reached the number of repetitions desired. Increasing the value of the loop counter each time the program segment within the loop has been executed.

We can understand while loop with the help of the following example:

/* 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 above program gets executed:

  • When the for statement is executed for the first time, the value of count is set to an initial value of 1.
  • Now the condition count <= 3 is tested. Since the 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 the count doesn’t exceed the final value 3.
  • When the 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.

3) The do-while Loop

The general form of for loop is as shown below:

The do-while loop looks like this:


do 
{ 
this ; 
and 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.