• Aucun résultat trouvé

Nesting For Loops

Dans le document Click to visit (Page 139-144)

Note You also could use the relational != (not equal) operator, changing the if statement to if (counter % 13 != 0 ).

Nesting For Loops

You can nest a for loop just as you can nest if statements. For example, the following program prints five rows of ten X characters:

#include <iostream>

With nested for loops, for each iteration of the outer for loop, the inner for loop goes through all its iterations. By analogy, in a clock, minutes are the outer loop, seconds the inner loop. In an hour, there are 60 iterations of minutes, but for each iteration of a minute, there are 60 iterations of seconds.

In the rows and columns example, for the first iteration of the outer for loop, the inner for loop goes through all ten of its iterations, printing ten X characters and one new line character. Then, for the next iteration of the outer for loop, the inner for loop again goes through all ten of its iterations, again printing ten X characters and one new line character.

The same thing happens on the third, fourth, and fifth iterations of the outer for loop, resulting in five rows of ten X characters.

While nested for loops can be used to print rows and columns for tables, they also have other uses. For example, the following program prompts the user for the total number of salespersons as well as the number of sales per salespersons, and has the user input

file:///D|/((%20CMH%20Decompiled%20))/C++%20Demystified/8151final/LiB0049.html (8 of 9)06.11.2004 22:51:54

The For Loop

each sale of each salesperson, and then afterward displays the average sale for each salesperson. The number of iterations of the outer for loop will be the number of salespersons. The number of iterations of the inner for loop will be the number of sales per salesperson.

#include <iostream>

using namespace std;

int main(void) {

int persons, int numSales;

cout << "Enter number of salespersons: ";

cin >> persons;

cout << "Enter number of sales per salesperson: ";

cin >> numSales;

The input and output could be

Enter number of salespersons: 2

Enter number of sales per salesperson: 3 Enter sale 1 for salesperson 1: 4

Enter sale 2 for salesperson 1: 5 Enter sale 3 for salesperson 1: 7

Average sales for salesperson #1 is 5.33333 Enter sale 1 for salesperson 2: 8

Enter sale 2 for salesperson 2: 3 Enter sale 3 for salesperson 2: 4 Average sales for salesperson #2 is 5

Note If you place a break or continue keyword in the inner loop, it will affect only that inner loop, and have no effect on the outer loop.

file:///D|/((%20CMH%20Decompiled%20))/C++%20Demystified/8151final/LiB0049.html (9 of 9)06.11.2004 22:51:54

Summary

Summary

You use a loop to repeat the execution of code statements. A loop is a structure that repeats the execution of code until a condition becomes false.

You learned in this chapter how to use one type of loop: the for loop. However, before discussing the for loop, I showed you how to use increment and decrement operators, which are used in for and other types of loops. I then explained the difference between prefix and postfix when using the increment and decrement operators.

You also learned in this chapter how to use the break keyword to prematurely terminate a for loop and the continue keyword to prematurely terminate the current iteration of the loop. You then learned how to use the logical operators as an alternative to the break and continue keywords. You also learned about nesting one for loop inside another.

The for loop generally is used when the loop will execute a fixed number of times.

However, sometimes the number of times a loop will execute is unpredictable, depending on user input during runtime. For example, in a data entry application, you may want a loop that, upon entry of invalid data, asks the user whether they want to retry or quit, and if they want to retry, gives the user another opportunity to enter data. The number of times this loop may execute is unpredictable, since it will keep repeating until the user either enters valid data or quits.

The next chapter will show you how to use two other types of loops, the while loop and the do while loop, that work better than a for loop when the number of times a loop will execute is unpredictable.

file:///D|/((%20CMH%20Decompiled%20))/C++%20Demystified/8151final/LiB0050.html06.11.2004 22:51:55

Quiz

Quiz

1. What does the increment operator do?

2. What does the decrement operator do?

3. Which occurs first, decrementing or the outputting of the value of num, in the statement cout << --num?

4. What is an iteration?

5. What is the usual purpose of the first expression in the parentheses following the for keyword?

6. What is the purpose of the second expression in the parentheses following the for keyword?

7. What is the usual purpose of the third expression in the parentheses following the for keyword?

8. Can one or more of the expressions in the parentheses following the for keyword be empty?

9. What is the purpose of the break keyword in a for loop?

10. What is the purpose of the continue keyword in a for loop?

11. If you were going to use nested for loops to print rows and columns, which for loop would print the columns—inner or outer?

file:///D|/((%20CMH%20Decompiled%20))/C++%20Demystified/8151final/LiB0051.html06.11.2004 22:51:56

Chapter 8: While and Do While Loops

Chapter 8: While and Do While Loops

Overview

The for loop generally is used when the loop will iterate a fixed number of times.

However, sometimes the number of times a loop will iterate is unpredictable, depending on user input during runtime. For example, in a data entry application, you may want a loop that, upon entry of invalid data, asks the user whether they want to retry or quit, and if they want to retry, gives the user another opportunity to enter data. The number of times this loop may iterate is unpredictable, since it will keep repeating until the user either enters valid data or quits.

This chapter will show you how to use the while loop, which is a better choice than a for loop when the number of times a loop will iterate is unpredictable.

While the total number of loop iterations may be unpredictable, there often are situations in which the loop will iterate at least once. An example is a loop that displays a menu with various choices, including exiting the program. In this menu example, the menu always displays at least once; the user cannot choose to exit before being given that choice. In such situations, a do while loop, which this chapter will show you how to use, is a better choice than a while loop.

file:///D|/((%20CMH%20Decompiled%20))/C++%20Demystified/8151final/LiB0052.html06.11.2004 22:51:57

Dans le document Click to visit (Page 139-144)