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 exit() Function in C Programming

The exit() Function

There is also a C library function, exit(), that can be used to cause a program to end. Because the exit() function is defined in a header file, stdlib.h, you have to include the header file at the beginning of your program।

The void Data Type in C Programming

The void Data Type

You may notice that the void word has been added into the C program in Listing 2.2. void is a keyword for a data type in C. When a void is placed prior to a function name, it indicates that the function does not return a value.

#include Directive and Header Files in C Programming

The #include Directive
Let's now move to line 2 in the C program of Listing 2.1:

Compiling and Linking C Programs

Compiling and Linking

Debugging C Program

Debugging Your Program