Presentation is loading. Please wait.

Presentation is loading. Please wait.

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++ (5) 10 September.

Similar presentations


Presentation on theme: "3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++ (5) 10 September."— Presentation transcript:

1 3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++ (5) 10 September 2008

2 3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 2 3.4 Program Flow Control

3 3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 3 if (cond) // if the cond { doA(); // is true, } // then do doA() else // else do doB() { doB(); } if... else 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. switch (expression) { case value1: doA(); break; case value2:doB(); break; } switch... case... break

4 3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 4 #include using namespace std; int main() { int firstNum, secondNum = 10; cout << "Please enter: \n"; cin >> firstNum; cout << "\n\n"; if (firstNum >= secondNum) {if ((firstNum% secondNum) == 0) { if (firstNum == secondNum) 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; } Nested if... else

5 3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 5 Using braces with if … else #include using namespace std; int main() { int x; cout 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; } ? ? What’s wrong with this program? How do you solve the problem? What’s wrong with this program? How do you solve the problem? { }

6 3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 6 switch … case … break #include using namespace std; int main() { unsigned short int number; cout << "Enter a number between 1 and 5: "; cin >> number; switch (number) {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 << "\n\n"; return 0; } Only accept number or expression that returns a number See the differences between break and without break Do the default if all tests fail.

7 3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 7 Implement switch with if … else 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"; } 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.

8 3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 8 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 3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 9 while Loops #include using namespace std; int main() { int counter = 0;//initialize counter while (counter<5) { counter++;//top of the loop cout << "counter: " << counter << "\n"; } cout << "Complete. Counter: " << counter << ".\n"; return 0; } while loops do the looping until the testing condition fails. Test condition ( tested variable must change inside the loop ) Repeat this part until counter >= 5 5

10 3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 10 More complicated while Loops while loops can be used with a more complicated testing condition. #include using namespace std; int main() { unsigned long small, large; unsigned short const MAXSMALL = 65535 ; cin>>small; cin>>large; while (small 0 && small < MAXSMALL) { if (small % 5000 == 0) cout << ".\n"; // write a dot every 5000 small++; large -= 2; } return 0; } 3 tests for each loop Testing parameters are updated in every loop

11 3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 11 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 using namespace std; int main() {int counter; cout << "How many hellos? "; cin >> counter; do {cout << "Hello\n"; counter--; }while (counter > 0); cout << "Counter is: " << counter << endl; return 0; } Hello will be shown at least once even if user enters 0 for counter

12 3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 12 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 using namespace std; int main() { int counter; for (counter=0; counter<5; counter++) cout << "Looping! "; //counter then increment cout << "\nCounter: " << counter << ".\n"; return 0; } Initialize Modify Test 5

13 3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 13 Different varieties of for loops #include using namespace std; int main() { for (int i=0, j=0; i<3; i++, j++) cout << "i: " << i << " j: " << j << endl; return 0; } Multiple initialization and increments #include using namespace std; int main() { int counter = 0; for (; counter < 5;) {counter++; cout<<"Looping! "; } cout << "\nCounter: " << counter << ".\n"; return 0; } Null initialization and increments

14 3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 14 Different varieties of for loops #include 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"; counter++;// increment } else break; //end for loop } return 0; } Empty for loop

15 3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 15 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. #include using namespace std; 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; } Execution of continue will skip the following part of the loop but not leaving the loop

16 3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 16 Nested for loops #include using namespace std; int main() {int rows, columns; char theChar; cout << "How many rows? "; cin >> rows; cout << "How many columns? "; cin >> columns; cout << "What characters? "; cin >> theChar; for (int i=0; i<rows; i++) {for (int j=0; j<columns; j++) cout << theChar; cout << "\n"; } return 0; } Nested for loop Will be executed ( rows  columns ) times More explanation on next page

17 3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 17 Value of i, j for (int i=0; i<rows; i++) {for (int j=0; j<columns; j++) cout << theChar; cout << "\n"; } Output on the screen: Assumption: rows = 2 columns = 3 theChar = x Assumption: rows = 2 columns = 3 theChar = x i = 0 j = ? i = 0 j = 0 x i = 0 j = 1 x i = 0 j = 2 x i = 0 j = 3 i = 1 j = 3 i = 1 j = 0 i = 1 j = 1 i = 1 j = 3 i = 1 j = 2 i = 1 j = 0 x i = 1 j = 1 x i = 1 j = 2 x  i = 0 j = 3  i = 1 j = 3 i = 2 j = 3

18 3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 18 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 3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 19 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?


Download ppt "3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++ (5) 10 September."

Similar presentations


Ads by Google