Structure of Programs

This lesson is concerned with helping you write programs that are a bit more complicated. Programs that have two or more loops. All the code shown belongs inside the body of main:
#include<stdio.h>
int main() {
   code goes here!
}
We just save a little space leaving it off.

There are two ways to combine two loops. The first is called sequence. In this the first loop starts and finishes before the second starts.
Example:

int i=0;
while (i<3) {
   statement1
   statement2
     . 
     . 
   i++;
}                      <-- bottom of loop 1 
int j=0;               <-- set up of variable for loop 2
while (j<5) {
   statementA
   statementB
     .
     .
   j++;
}
When this program runs, ALL the repetitions of the first loop are completely finished before C starts the repetitions of the second loop. In particular the first statement after the compound statement (body) of the first loop,
int j=0;
is not executed until after the repetitions of the first loop are done.

Terminology: The body of a loop is statement coming after the condition. It is the statement that is controled by the condition. That is, if the condition is true, we execute the body and re-check the condition. If the condition is false, we skip the body. The while together with its condition is called the header of the loop.

The second way to combine two loops is called nested. In this the second loop is completely contained in the body of the first loop.
Example:

int i=0;                    <-- set up for first loop0
while (i<3) {               <-- first loop header
   statement1
   statement2
   
   int j=0;            <-- set up for second loop
   while (j<5) {       <-- second loop header
      statementA
      statementB
      
      j++;          
   }                   <-- second loop bottom 
   i++;          
}                           <-- first loop bottom 
When this program runs, each repetition of the outside (first) loop restarts the inside loop, so we get all the repetitions of the inside loop for each repetition of the outer loop. The outer loop starts the inside loop a total of 3 times. Each time the inside loop is started statementA is executed 5 times. So in all statementA is executed a total of 15 times.

IMPORTANT: C regards the loop header together with its body as a unit. Executing this unit means running it until the while-condition becomes false.

Suppose this unit is nested inside another loop. That is, the unit is one of the statements in body of the outside loop. When the inside unit finishes executing one of three things happens. If it is not the last statement inside the (outside) loop, then execution just goes on to the next statement in the outside loop. If, however, the unit is the last statement inside of the outside loop, we either get another repetition of the outside loop if the outside loop is not finished, or, if the outside loop is finished, we go on to whatever statement comes after the outside loop.

A Sequence Problem

Problem: Write a program to print some lines that say Hello and then some lines that say Goodbye. First it should ask the user for the number of Hello's and the number of Goodbye's.

Solution: The first part is pretty easy:

int numHel;
scanf("%d", &numHel);
int numGB;
scanf("%d", &numGB);
The above must be followed by the sequence: a loop to print the Hello's followed by a loop to print the Goodbye's. Here is the complete program:
int numHel;
scanf("%d", &numHel);
int numGB;
scanf("%d", &numGB);
int i=0;
while (i<numHel) {
   printf("Hello\n");
   i++;
}
int j=0;
while (j<numGB) {
   printf("Goodbye\n");
   j++;
}

Next: Solving Nested Loop Problems