1 Computer Programming 3 The Nuts and Bolts
1 Computer Programming 3. The Nuts and Bolts of C++ Learning the C++ language 3. The Nuts and Bolts of C++ (5) 10 September 2008
2 Computer Programming 3. The Nuts and Bolts of C++ 3. 4 Program Flow Control
3 Computer Programming 3. The Nuts and Bolts of C++ Control of Program Flow • Normal program execution is performed from top-to-down and statement-by-statement. • It is often that the program modifies the program flow depending on some conditions set by the programmer or user. • C++ provides many approaches to control program flow. if. . . else if (cond) // if the cond { do. A(); // is true, } // then do do. A() else // else do do. B() { do. B(); } switch. . . case. . . break switch (expression) { case value 1: do. A(); break; case value 2: do. B(); break; }
4 Computer Programming 3. The Nuts and Bolts of C++ #include <iostream> using namespace std; int main() Nested if. . . { int first. Num, second. Num = 10; cout << "Please enter: n"; cin >> first. Num; cout << "nn"; if (first. Num >= second. Num) { if ((first. Num% second. Num) == 0) { if (first. Num == second. Num) cout << "They are the same!n"; else cout << "They are evenly divisible!n"; } else cout << "They are not evenly divisible!n"; } else cout << "Hey! The second no. is larger!n"; return 0; } else
5 Computer Programming 3. The Nuts and Bolts of C++ Using braces with if … else #include <iostream> What’s wrong with this program? using namespace std; How do you solve the problem? int main() { int x; cout << "Enter a number x < 10 or x > 100: "; cin >> x; cout << "n"; ? if (x >= 10) { if (x > 100) cout << "More than 100, thanks!n"; } ? else // not the else intended! cout << "Less than 10, thanks!n"; return 0; }
6 Computer Programming 3. The Nuts and Bolts of C++ switch … case … break #include <iostream> Only accept number or expression using namespace std; that returns a number int main() { unsigned short int number; cout << "Enter a number between 1 and 5: "; cin >> number; switch (number) See the differences between break and without break { case 0: cout << "Too small, sorry!"; break; case 3: cout << "Excellent!n"; //falls through case 2: cout << "Masterful!n"; //falls through case 1: cout << "Incredible!n"; break; default: cout << "Too large!n"; break; } cout << "nn"; return 0; Do the default if all tests fail. }
7 Computer Programming 3. The Nuts and Bolts of C++ Implement switch with if … else • switch … case statement can be implemented by the if … else statement but with more complicated structure. • However, switch … case can only be applied to simple numerical tests. if (num == 0) cout << "Too small, sorry!"; else { if (num == 3 || num == 2 || num == 1) { cout << "Incredible!n"; if (num == 3 || num == 2) { cout << "Masterful!n"; if (num == 3) cout << "Excellent!n"; } } else cout << "Too large!n"; }
8 Computer Programming 3. The Nuts and Bolts of C++ Looping • Many programming problems are solved by repeatedly acting on the same data. • The method for achieving repeated execution is by looping. • C++ provides many approaches to implement looping • if … goto // old way, strongly NOT recommended • while loop // use when the testing parameters are // complicated • do-while loop // use when the repeated part is expected // to be executed at least once • for loop // use when the testing parameters are // simple
9 Computer Programming 3. The Nuts and Bolts of C++ while Loops • while loops do the looping until the testing condition fails. #include <iostream> using namespace std; int main() Test condition (tested variable must change inside the { int counter = 0; //initialize counter while (counter<5) { counter++; //top of the loop cout << "counter: " << counter << "n"; } cout << "Complete. Counter: " << counter << ". n"; return 0; } Repeat this part until counter >= 5 5 loop
10 Computer Programming 3. The Nuts and Bolts of C++ More complicated while Loops • while loops can be used with a more complicated testing condition. #include <iostream> using namespace std; int main() { unsigned long small, large; unsigned short const MAXSMALL = 65535 ; cin>>small; cin>>large; while (small < large && large > 0 && small < MAXSMALL) { if (small % 5000 == 0) cout << ". n"; // write a dot every 5000 small++; 3 tests for each loop large -= 2; } Testing parameters are return 0; updated in every loop }
11 Computer Programming 3. The Nuts and Bolts of C++ do … while • while loops do the test first and then the loop. • Sometimes we would like to have the loop to be done at least once. In this case, do … while can be used. #include <iostream> Hello will be shown at using namespace std; least once even if user int main() enters 0 for counter { int counter; cout << "How many hellos? "; cin >> counter; do { cout << "Hellon"; counter--; } while (counter > 0); cout << "Counter is: " << counter << endl; return 0; }
12 Computer Programming 3. The Nuts and Bolts of C++ for loops • In many loops, we initialize a testing parameter, modify the value of the parameter and test the parameter. • It can be done in a single line by a for loop. #include <iostream> using namespace std; int main() { Initialize Test Modify int counter; for (counter=0; counter<5; counter++) cout << "Looping! "; //counter then increment cout << "n. Counter: " << counter << ". n"; return 0; } 5
13 Computer Programming 3. The Nuts and Bolts of C++ Different varieties of for loops #include <iostream> Multiple initialization and using namespace std; int main() { for (int i=0, j=0; i<3; i++, j++) cout << "i: " << i << " j: " << j << endl; return 0; } #include <iostream> using namespace std; int main() Null initialization and { int counter = 0; for (; counter < 5; ) { counter++; cout<<"Looping! "; } cout << "n. Counter: " << counter << ". n"; return 0; } increments
14 Computer Programming 3. The Nuts and Bolts of C++ Different varieties of for loops Empty for #include <iostream> using namespace std; int main() { int counter = 0; // initialization int max; cout << "How many hellos? "; cin >> max; for (; ; ) // a for loop that doesn't end { if (counter < max) // test { cout << "Hello!n"; loop counter++; // increment } else break; //end for loop } return 0; }
15 Computer Programming 3. The Nuts and Bolts of C++ continue and break • Keywords continue and break allow one to change the program flow during looping. • Should be used with caution since it will make the program hard to understand owing to the sudden change of direction. Execution of continue will skip the following #include <iostream> using namespace std; part of the loop but not leaving the loop int main() { int counter; for (counter=0; counter<5; counter++) { cout << "Looping! counter is " << counter <<"n"; if ((counter%2) == 1) //odd number continue; cout << "Counter is an even number. n"; } return 0; }
16 Computer Programming 3. The Nuts and Bolts of C++ Nested for loops #include <iostream> using namespace std; int main() { int rows, columns; char the. Char; cout << "How many rows? "; More cin >> rows; explanatio cout << "How many columns? "; n on next cin >> columns; page cout << "What characters? "; cin >> the. Char; for (int i=0; i<rows; i++) { for (int j=0; j<columns; j++) Nested for cout << the. Char; loop cout << "n"; Will be executed } return 0; (rows columns) } times
17 Computer Programming 3. The Nuts and Bolts of C++ for (int i=0; i<rows; i++) Assumption: { for (int j=0; j<columns; j++) rows = 2 cout << the. Char; columns = 3 cout << "n"; the. Char = x } Value of i, j i i i i i = = = = = 0 0 0 0 0 j j j j j = = = = = ? 0 0 1 1 2 2 3 3 i i i i i = = = = = 1 1 1 1 1 2 j j j j j = = = = = 3 0 0 1 1 2 2 3 3 3 Output on the screen: xxx
18 Computer Programming 3. The Nuts and Bolts of C++ l If a variable is defined in the initialization part of the for loop, the variable will no longer exist on leaving the for loop. It is not an error for Visual Studio. NET 2003.
19 Computer Programming 3. The Nuts and Bolts of C++ Exercise 3. 4 For the program on p. 16, a. Build the project and note the result. b. Try to rewrite the program using nested while loops instead of the nested for loops. Which program is more complicated?
- Slides: 19