CS 1440 Lab 9

Cover sheetPrevious ActivityNext Activity


Activity 9-3:

Activity Goals

Activity Procedure

The for Statement

We have previously used two loop statements: while and do..while. Now we'll introduce a third: the for statement.

Note: Any loop can be written with the while statement. You can survive nicely without ever using do..while or for. However, you'll see these alternatives often because they can make programs a little shorter and more readable. Just make sure you're an expert at using while before complicating your life with the other options.

The general form is:

for(initialization statement; loop condition; increment step)
        statement;

For example,

for(i=0; i<10; i++)
   cout << i << endl;

This would print the integers 0 through 9. It's just like saying:

i=0;
while(i<10)
{
        cout << i << endl;
        i++;
}

The general form shown above is just like:

initialization statement;
while(loop condition)
{
        statement;
        increment step;
}

While we could get by just fine with the while form, this is a very typical loop and it's nice to be able to compress it.

You may need to do several things for each iteration of the loop, in which case a compound statement is used; e.g.

for(i=0; i<10; i++)
{
        x=2*i;
        cout << i << " " << x << endl;
}

(What do you think this does? If you have any doubts, test the code in a short program.)

Nested Loops

What do you think the following code will do?
#include <iostream>
int main()
{
        int i,j;
        i = 1;
        while (i<=5)
        {
                j = 1;
                while (j<=10)
                {
                        cout << "*";
                        ++j;
                }
                cout << endl;
                ++i;
        }
        return 0;
}
Copy the code to a file called box.C, compile and run it. Let's talk about its behavior. The outer while loop runs 5 times, doesn't it? Each time it runs, 4 things happen: j gets set to 1, the inner loop runs 10 times, a newline is output, and i is increased by 1. What does the inner loop do each time it runs? It prints a star then adds 1 to j. Thus the total effect is to make 5 rows of stars with 10 stars in each row.

Think back to the program draw.C which you wrote for lab 5. We had a function called make_row which contained a loop. That function's job was similar to the job of the inner loop in the above program. We had another function called draw_rect that contained a loop. Inside of its loop it called make_row. The overall effect, then, was like a loop within a loop. We could have used nested loops inside draw_rect and draw_tri, and not have written the make_row function at all.

for loops can be nested just like while loops. Here is the same program done with for loops:

#include <iostream>
int main()
{
        int i,j;
        for(i=1; i<=5; i++)
        {
                for(j=1; j<=10; j++)
                        cout << "*";
                cout << endl;
        }
        return 0;
}

Copy this program to a file called box2.C, compile and run it.

What would happen if we changed "j<=10" to "j<=i"? Try to figure out the answer to that question, then change the program and run it to see if you're right.

Here's another challenge: what is the output of this program?

#include <iostream>
int main()
{
        int i,j;
        for(i=1; i<=5; i++)
        {
                for(j=5; j>0; j--)
                        cout << i << j << " ";
                cout << endl;
        }
        return 0;
}

Run the program to check your guess.