I l@ve RuBoard |
![]() ![]() |
8.1 for StatementThe for statement allows you to execute a block of code a specified number of times. The general form of the for statement is: for (initial-statement; condition; iteration-statement) body-statement; This is equivalent to: initial-statement; while (condition) { body-statement; iteration-statement; } For example, Example 8-1 uses a while loop to add five numbers. Example 8-1. total6/total6w.cpp#include <iostream> int total; // total of all the numbers int current; // current value from the user int counter; // while loop counter int main( ) { total = 0; counter = 0; while (counter < 5) { std::cout << "Number? "; std::cin >> current; total += current; ++counter; } std::cout << "The grand total is " << total << '\n'; return (0); } The same program can be rewritten using a for statement, as seen in Example 8-2. Example 8-2. total6/total6.cpp#include <iostream> int total; // total of all the numbers int current; // current value from the user int counter; // for loop counter int main( ) { total = 0; for (counter = 0; counter < 5; ++counter) { std::cout << "Number? "; std::cin >> current; total += current; } std::cout << "The grand total is " << total << '\n'; return (0); } Note that counter goes from 0 to 4. Normally you count five items as 1, 2, 3, 4, 5. You will get along much better in C++ if you change your thinking to zero-based counting and count five items as 0, 1, 2, 3, 4. (One-based counting is one of the main causes of array overflow errors. See Chapter 5.) Careful examination of the two flavors of this program reveals the similarities between the two versions, as shown in Figure 8-1. Figure 8-1. Similarities between while and for![]() Many older programming languages do not allow you to change the control variable (in this case counter) inside the loop. C++ is not so picky. You can change the control variable anytime you wish—you can jump into and out of the loop and generally do things that would make a PASCAL or FORTRAN programmer cringe. (Even though C++ gives you the freedom to do such insane things, that doesn't mean you should do them.) Question 8-1: What is the error in Example 8-3? Example 8-3. cent/cent.cpp#include <iostream> /* * This program produces a Celsius to Fahrenheit conversion * chart for the numbers 0 to 100. * * Restrictions: * This program deals with integers only, so the * calculations may not be exact. */ // the current Celsius temperature we are working with int celsius; int main( ) { for (celsius = 0; celsius <= 100; ++celsius); std::cout << "celsius: " << celsius << " Fahrenheit: " << ((celsius * 9) / 5 + 32) << '\n'; return (0); } When run, this program prints out: Celsius: 101 Fahrenheit: 213 and nothing more. Why? Question 8-2: Example 8-4 reads a list of five numbers and counts the number of threes and sevens in the data. Why does it give us the wrong answers? Example 8-4. seven/seven.cpp#include <iostream> int seven_count; // number of sevens in the data int data[5]; // the data to count 3 and 7 in int three_count; // the number of threes in the data int the_index; // index into the data int main( ) { seven_count = 0; three_count = 0; std::cout << "Enter 5 numbers\n"; std::cin >> data[1] >> data[2] >> data[3] >> data[4] >> data[5]; for (the_index = 1; the_index <= 5; ++the_index) { if (data[the_index] == 3) ++three_count; if (data[the_index] == 7) ++seven_count; } std::cout << "Threes " << three_count << " Sevens " << seven_count << '\n'; return (0); } When we run this program with the data 3 7 3 0 2, the results are: Threes 4 Sevens 1 ![]() |
I l@ve RuBoard |
![]() ![]() |