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

Exercises : Answer the following Question

After Reading and Understanding the Chapter 01 : How To Start C programming, please give the answer of following Questions. If you able to answer these question correctly then please proceed to Write My First C Program.

Question and Answer

Questions and Answers on Chapter 03 : "The Essentials of C Programs". Some Questions and their answers are given bellow for better understanding of Chapter 03 : The Essentials of C Programs . If any body ask any question please write in comment page or forum.

C প্রোগ্রাম Function এর ভিতরের অংশের আলোচনা

The Function Body

The function body in a function is the place that contains variable declarations and C statements. The task of a function is accomplished by executing the statements inside the function body one at a time.

C প্রোগ্রামিংয়ে Function কে কিভাবে কল করবেন ?

Making Function Calls

Based on what you've learned so far, you can write a C program that calls the integer_add() function to calculate an addition and then print out the result on the screen. An example of such a program is demonstrated in Listing 3.2.