Nesting of Loops in C

The way if statements can be nested, similarly whiles and for can also be nested.

To understand how nested loops work, look at the program given below:

/* Example 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 ) ;

After running this program you will get the following 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

Here, for each value of r the inner loop is cycled through twice, with the variable c taking values from 1 to 2.

The inner loop terminates when the value of c exceeds 2, and the outer loop terminates when the value of r exceeds 3.

As you can see, the body of the outer for loop is indented, and the body of the inner for loop is further indented. These multiple indentations make the program easier to understand. Instead of using two statements, one to calculate sum and another to print it out, we can compact this into one single statement by saying:
printf ( “r = %d c = %d sum = %d\n”, r, c, r + c ) ;

The way for loops have been nested here, similarly, two while loops can also be nested. Not only this, a for loop can occur within a while loop, or a while within a for.

Multiple Initialisations in the for Loop

The initialisation expression of the for loop can contain more than one statement separated by a comma.
For example,

for ( i = 1, j = 2 ; j <= 10 ; j++ )

Multiple statements can also be used in the incrementation expression of for loop; i.e., you can increment (or decrement) two or more variables at the same time. However, only one expression is allowed in the test expression. This expression may contain several conditions linked together using logical operators.

Use of multiple statements in the initialisation expression also demonstrates why semicolons are used to separate the three expressions in the for loop. If commas had been used, they could not also have been used to separate multiple statements in the initialisation expression, without confusing the compiler.

Related Posts

  • 69
    It is perfectly all right if we write an entire if-else construct within either the body of the if statement or the body of an else statement. This is called ‘nesting’of ifs. Let's have an example: /* A quick demo of nested if-else */ main( ) { int i ;…
    Tags: nested, program, nesting, learn, tutorial, programming
  • 57
    Uses of Odd Loop In real life programming one comes across a situation when it is not known beforehand how many times the statements in the loop are to be executed. This situation can be programmed as shown below: /* Execution of a loop an unknown number of times */…
    Tags: loop, learn, programming, tutorial
  • 53
    For loop is probably the most popular looping instruction. 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…
    Tags: loop, learn, programming
  • 50
    The programs that we have learned/developed in this tutorial 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…
    Tags: loop, program, loops, tutorial, programming, learn
  • 48
    The if statement can take any of the following forms: if ( condition ) do this ; if ( condition ) { do this ; and this ; } if ( condition ) do this ; else do this ; if ( condition ) { do this ; and this…
    Tags: learn, program, programming, tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: