Submitted by tushar pramanick on Tue, 03/05/2013 - 20:14

Using Nested Loops

You can put a loop inside another one to make nested loops. The computer will run the inner loop first before it resumes the looping for the outer loop.

Listing 7.7 is an example of how nested loops work.

 

TYPE
Listing 7.7. Using nested loops.


1:  /* 07L07.c: Demonstrating nested loops */
2:  #include <stdio.h>
3:
4:  main()
5:  {
6:     int i, j;
7:
8:     for (i=1; i<=3; i++) {   /* outer loop */
9:        printf("The start of iteration %d of the outer loop.\n", i);
10:       for (j=1; j<=4; j++)  /* inner loop */
11:          printf("    Iteration %d of the inner loop.\n", j);
12:       printf("The end of iteration %d of the outer loop.\n", i);
13:    }
14:    return 0;
15: }


    OUTPUT
    The following result is obtained by running the executable file 07L07.exe:

    C:\app> 07L07
    The start of iteration 1 of the outer loop.
        Iteration 1 of the inner loop.
        Iteration 2 of the inner loop.
        Iteration 3 of the inner loop.
        Iteration 4 of the inner loop.
    The end of iteration 1 of the outer loop.
    The start of iteration 2 of the outer loop.
        Iteration 1 of the inner loop.
        Iteration 2 of the inner loop.
        Iteration 3 of the inner loop.
        Iteration 4 of the inner loop.
    The end of iteration 2 of the outer loop.
    The start of iteration 3 of the outer loop.
        Iteration 1 of the inner loop.
        Iteration 2 of the inner loop.
        Iteration 3 of the inner loop.
        Iteration 4 of the inner loop.
    The end of iteration 3 of the outer loop.
    C:\app>

    ANALYSIS
    In Listing 7.7, two for loops are nested together. The outer for loop starts in line 8 and ends in line 13, while the inner for loop starts in line 10 and ends in line 11.

The inner loop controls one statement that prints out the iteration number according to the numeric value of the integer variable j. As you see in line 10, j is initialized with 1, and is increased by 1 after each looping (that is, iteration). The execution of the inner loop stops when the value of j is greater than 4.

Besides the inner loop, the outer loop has two statements in lines 9 and 12, respectively. The printf() function in line 9 displays a message showing the beginning of an iteration from the outer loop. An ending message is sent out in line 12 to show the end of the iteration from the outer loop.

From the output, you can see that the inner loop is finished before the outer loop starts another iteration. When the outer loop begins another iteration, the inner loop is encountered and run again. The output from the program in Listing 7.7 clearly shows the execution orders of the inner and outer loops.

WARNING

    Don't confuse the two relational operators (< and <=) and misuse them in the expressions of loops.
    For instance, the following

    for (j=1; j<10; j++){
        /* statement block */
    }
    for (j=1; j<=10; j++){
        /* statement block */
    }


    the total number of iterations is 10 because the relational expression j<=10 is evaluated in this case. Note that the expression returns 1 as long as j is less than or equal to 10.
    Therefore, you see the difference between the operators < and <= causes the looping in the first example to be one iteration shorter than the looping in the second example.

Related Items

The if-else Statement

The if-else Statement

The if statement

The if statement

If life were a straight line, it would be very boring. The same thing is true for programming. It would be too dull if the statements in your program could only be executed in the order in which they appear.

Mathematical Functions in C

Mathematical Functions in C

Basically, the math functions provided by the C language can be classified into three groups:

    Trigonometric and hyperbolic functions, such as acos(), cos(), and cosh().

Changing Data Sizes

Changing Data Sizes

Enabling or Disabling the Sign Bit

Enabling or Disabling the Sign Bit

As you know, it's very easy to express a negative number in decimal. All you need to do is put a minus sign in front of the absolute value of the number. But how does the computer represent a negative number in the binary format?