1 / 48

Branching

Branching. Condition. Statement list 1. T. Condition. Statement list. T. F. F. Statement list 2. The Syntax of if and if/else statements. A Boolean expression (logical expression). In C++, 0 is false , any non-zero values will be considered as true. if ( condition ) {

ejesse
Télécharger la présentation

Branching

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Branching Condition Statement list 1 T Condition Statement list T F F Statement list 2 IT 279, Chung-Chih Li

  2. The Syntax of if and if/else statements A Boolean expression (logical expression). In C++, 0 is false, any non-zero values will be considered as true. if ( condition ) { statement list; } Reserved words A reserved word can’t be used as an identifier true false if ( condition ) { statement list1; } else { statement list2; } Indentation indicates that the statements in the statement list are at the level next to the if/else statement. IT 279, Chung-Chih Li

  3. max II #include <iostream> using namespace std; void main() { int a, b, c; cin >> a >> b >> c; if (a > b) { } else { } cout << “ is the biggest”; } if (a > c) cout << “a:” << a; else cout << “c:” << c; if (b > c) cout << “b:” << b; else cout << “c:” << c; IT 279, Chung-Chih Li

  4. Operators • Arithmetic operators: + - / * % • Relational operators: == > < <= >= != • Logical operators: || && ! IT 279, Chung-Chih Li

  5. == > < <= >= != Relational Operators cout << (1 < 0) << endl; 0 cout << (1 > 0) << endl; 1 cout << (1 == 0) << endl; 0 cout << (1 <= 0) << endl; 0 cout << (1 >= 0) << endl; 1 cout << ("1" > "0") << endl; 1 cout << ("Yes" == "yes") << endl; 0 cout << ("aab" > "aaa") << endl; 1 cout << (2 < 3 < 4) << endl; cout << (4 > 3 > 2) << endl; cout << (4 > (3 > 2)) << endl; cout << (0 < 0.5 < 0.6) << endl; 1 0 1 0 IT 279, Chung-Chih Li

  6. || && ! Logical Operators Assume x = 10 true (1 || 0) ((18 <= x) && (x <= 50)) ((18 <= x) || (x <= 50)) !(x < 5) is same as (x >= 5) (((x % 2) == 0) && ((x % 3) == 0)) false true true false IT 279, Chung-Chih Li

  7. De Morgan’s law I am not a female student.  I am not female or I am not a student. I will not be in my office or in the lab.  I will not be in my office and will not be in the lab. !(A && B) is same as !A || !B !(A || B) is same as !A && !B IT 279, Chung-Chih Li

  8. Nested if/else statement if ( condition 1 ) { statement list; } else { statement list; } if ( condition 2 ) { statement list; } else { statement list; }; statement list; Indentation indicates the level of statements. IT 279, Chung-Chih Li

  9. Example of nested if/else statement cout << "How many items do you want to buy? "; cin >> a; if (a == 1) discount = 0.1; else { if (a == 2) discount = 0.2; else { cout << "At most two items!!"; } } IT 279, Chung-Chih Li

  10. Enumeration data type int i,j; enumdays {Mon, Tue, Wed, The, Fri, Sat, Sun}; enumdays d1, d2=Wed; ....... d1=d2; if (d1 < Sat) cout << d1 << “It is a week day”; else cout << d1 << “It is a weekend”; IT 279, Chung-Chih Li

  11. Enumeration data type II enumdays {Mon=1, Tue=2, Wed=3, The=4, Fri=5, Sat=6, Sun=7} d1; ....... if (d1 < Sat) cout << d1 << “It is a week day”; else cout << d1 << “It is a weekend”; enumdays {Mon=5, Tue=4, Wed=3, The=2, Fri=1, Sat=0, Sun=0} d1; ....... if (d1 != 0) cout << d1 << “It is a week day”; else cout << d1 << “It is a weekend”; IT 279, Chung-Chih Li

  12. Ambiguity in English I saw the girl with a telescope. I saw the girl with her boy friend. IT 279, Chung-Chih Li

  13. Ambiguity in C++ // A correct way if (a_member) { if (married) { ..... } } else { ..... } bool a_member, married; // You don’t mean this: ..... if (a_member) if (married) { cout << “Input your spouse’s name:”; cin >> sname; } else cout << “Sorry, can’t get in!!”; IT 279, Chung-Chih Li

  14. Confusing nested if/else statement bool weekend; enum days = {Mon, Tue, Wed, The, Fri, Sat, Sun}; enum days d1=Mon, d2=Sun; ....... d1 = Sun; if (d1 < Sat) if (d1 == Mon) cout << “Have a nice week!!\n”; else cout << “have a nice weekend\n”; cout << “end\n”; if (d1 < Sat) { if (d1 == Mon) cout << “Have a nice week!!\n”; } else cout << “have a nice weekend\n”; cout << “end\n”; IT 279, Chung-Chih Li

  15. Cascaded if/else statements if ( condition_1 && condition_2 && condition_3 && condition_4 && condition_5 && condition_6) statement_1; else statement_2; if (condition_1) if(condition_2) if(condition_3) if(condition_4) if(condition_5) if(condition_6) statement_1; else statement_2; = = if (condition_1) { if(condition_2) if (condition_3) if(condition_4) if(condition_5) if(condition_6) statement_1; else statement_2; } IT 279, Chung-Chih Li

  16. Other forms of Nested if/else statements (Cascaded) I if (condition_1) statement_1; else if (condition_2) statement_2; else if (condition_3) statement_3; else if (condition_4) statement_4; else if (condition_5) statement_5; else if (condition_6) statement_6; if (condition_1) statement_1; if (condition_2) statement_2; if (condition_3) statement_3; if (condition_4) statement_4; if (condition_5) statement_5; if (condition_6) statement_6; V.S. IT 279, Chung-Chih Li

  17. Other forms of Nested if/else statements (Cascaded) II if ( condition_1 && condition_2 && condition_3 && condition_4 && condition_5 && condition_6) statement_1; else statement_2; if (condition_1) if (condition_2) if (condition_3) if (condition_4) if (condition_5) if (condition_6) statement_1; else statement_2; = IT 279, Chung-Chih Li

  18. Switchvs.Cascaded if/else if (i == 1) statement_1; else if (i == 2) statement_2; else if (i == 3) statement_3; else if (i == 4) statement_4; else if (i == 5) statement_5; else if (i == 6) statement_6; switch (i) { case 1: statement_1; break; case 2: statement_2; break; case 3: statement_3; break; case 4: statement_4; break; case 5: statement_5; break; case 6: statement_6; break; } IT 279, Chung-Chih Li

  19. Example of switch cout << "Input an integer as the day of the week:"; cin >> i; switch (i) { case 1 : cout << "\n Sunday"; break; case 2 : cout << "\n Monday"; break; case 3 : cout << "\n Tuesday"; break; case 4 : cout << "\n Wednesday"; break; case 5 : cout << "\n Thursday"; break; case 7 : cout << "\n Saturday"; break; case 6 : cout << "\n Friday"; break; } IT 279, Chung-Chih Li

  20. breaks in a Switchstatement switch (i) { case 1: statement_1; break; case 2: statement_2; case 3: statement_3; break; case 4: statement_4; } if (i == 1) statement_1; else if (i == 2) { statement_2; statement_3; } else if (i == 3) statement_3; else if (i == 4) statement_4; IT 279, Chung-Chih Li

  21. Branching Loops Condition Statement list Condition T Statement list T F F IT 279, Chung-Chih Li

  22. while Condition while (Condition) { Statement list } Statement list T F IT 279, Chung-Chih Li

  23. Example 1: while (ans != “Y” || ans != “y”) string ans = “n”; while (ans != “Y” && ans != “y”) { cout << “Would you marry me?”; cin >> ans; } cout << “Great!!”; Can I put ; here? No!! Should I put ; here? Up to you!! IT 279, Chung-Chih Li

  24. Example 2: while Will there be any problem? int no_times; cout << “How many times do you want to say?”; cin >> no_times; while (no_times != 0) { cout << “Hello!” << endl; no_times--; } cout << “End!!” << endl; What if one inputs –1? while (no_times > 0) IT 279, Chung-Chih Li

  25. Example 3: while int a,b,sum; cout << “This program will return the ”; cout << “summation of integers from a to b.\n\n”; cout << “Input two integers a and b:”; cin >> a >> b; while (a <= b) { sum += a; a++; } cout << “The sum is ” << sum << endl; sum = 0; Don’t forget to set sum = 0; sum = sum + a; IT 279, Chung-Chih Li

  26. Example 4: while int a,b,sum=0; cout << “This program will return the sum ”; cout << “of odd numbers between a and b.\n\n”; cout << “Input two integers a and b:”; cin >> a >> b; while (a <= b) { if (a % 2) sum += a; a++; } cout << “The answer is ” << sum << endl; if (a % 2 == 0) a++; while (a <= b) { sum += a; a += 2; } 3, 4, 5, 6 , 7 a b 2, 3, 4, 5, 6 , 7 IT 279, Chung-Chih Li

  27. Example 5: while 3N+1 problem long n,i=0; cout << “Input an integer:”; cin >> n; while (n > 1) { if (n % 2) n = 3*n+1; else n /= 2; cout << ++i << “:” << n << endl; } cout << “Done!!” << endl; Input an integer:7 1:22 2:11 3:34 4:17 5:52 6:26 7:13 8:40 9:20 10:10 11:5 12:16 13:8 14:4 15:2 16:1 Done!! Press any key to continue Input an integer:11 1:34 2:17 3:52 4:26 5:13 6:40 7:20 8:10 9:5 10:16 11:8 12:4 13:2 14:1 Done!! Press any key to continue Input an integer:3759 1:11278 2:5639 3:16918 4:8459 5:25378 6:12689 7:38068 ..... ..... 83:16 84:8 85:4 86:2 87:1 Done!! Press any key to continue Will we always get out of a loop? That is the question!! No one knows! IT 279, Chung-Chih Li

  28. do-while string ans = “n”; while (ans != “Y”) { cout << “Would you marry me?”; cin >> ans; } cout << “Great!!”; do { cout << “Would you marry me?”; cin >> ans; } while (ans != “Y”); cout << “Great!!”; Statement list Condition T do { Statement list } while (Condition); F ; is required IT 279, Chung-Chih Li

  29. Example 1: do-while int i; .... do { cout << “Please input a number between ” << “10 and 20:”; cin >> i; } while (i < 10 || i > 20); ...... IT 279, Chung-Chih Li

  30. Primality Test A prime number is a positive integer that cannot be factorized, i.e., no numbers other that 1 and itself can divide it. Is (893 % 2 == 0) ? Is (893 % 3 == 0) ? Is (893 % 4 == 0) ? Is (893 % 5 == 0) ? Is (893 % 6 == 0) ? . . . Is (893 % 892 == 0) ? is 893 a prime? We can write a loop to test these. IT 279, Chung-Chih Li

  31. Example: SillyPrimality Test long int p,r,i=2; cout << “Input an positive integer:”; cin >> p; cout << p << “ is “; do { r = p % i; i++; } while (i < p && r != 0); if (i == p) cout << “a prime number.”; else { cout << “not a prime number.”; cout << “The least factor is ” << --i; } IT 279, Chung-Chih Li

  32. Break in a loop The break statement in a loop will force the program to jump out of the loop immediately. do { r = p % i; if (r == 0) break; i++; } while (i < p); do { cout << “Would you marry me?”; cin >> ans; cout << “Really?” cin >> ans; } while (ans != “Y” && ans != “y”); cout << “Great!!”; if (ans == “F” || and == “f”) break; IT 279, Chung-Chih Li

  33. Continue in a loop The continue statement in a loop will force the program to check the loop condition immediately. do { cout << “Would you marry me?”; cin >> ans; if (and != “Y” && ans != “y”) continue; cout << “Great?” break; } while (true); .... IT 279, Chung-Chih Li

  34. #include <iostream> using namespace std; void main() { int a,b; cout << "Input two positive integers:"; cin >> a >> b; int r = a % b; while (r) { a = b; b = r; r = a % b; } cout << "The GCD is " << b << ".\n"; } Euclid Algorithm IT 279, Chung-Chih Li

  35. Primality Test with while loop #include <iostream> using namespace std; void main() { bool is_prime=true; int d=2,p; cout << "Input a positive integers:"; cin >> p; while (d <= p/2) { if (p % d == 0) { is_prime=false; break; } d++; } if (is_prime) ... } Can we change to while (d < p/2)? IT 279, Chung-Chih Li

  36. Primality Test with do-while loop #include <iostream> using namespace std; void main() { bool is_prime=true; int d=2,p; cout << "Input a positive integers:"; cin >> p; do { if (p % d == 0) { is_prime=false; break; } d++; } while (d <= p/2); if (is_prime) ... } Can we change to while (d < p/2)? IT 279, Chung-Chih Li

  37. Primality Test with do-while loop (a bit better) void main() { bool is_prime=true; intd=3,p; cout << "Input a positive integers:"; cin >> p; do { if((p % d == 0) || (p % 2 == 0)) { is_prime=false; break; } d += 2; } while (d < p/2); if (is_prime) ... } IT 279, Chung-Chih Li

  38. Primality Test with do-while loop (yet another improvement) void main() { bool is_prime=true; intd=3,p; cout << "Input a positive integers:"; cin >> p; if (p % 2 == 0} is_prime=false; elsedo { if (p % d == 0) { is_prime=false; break; } d += 2; } while (d < p/2); if (is_prime) ... } What if I forget else here? IT 279, Chung-Chih Li

  39. Definite Loop • In programming a definite loop is more welcome. I.e., number of iterationsisknown before the loop begins, at least the upper bound is known. I.e., repeat the loop 100 times. Precisely speaking, there is no definite loop in C++ IT 279, Chung-Chih Li

  40. The general format for a for loop for(Initialization_action;Condition;Condition_update) { statement_list; } 1 2 3 Factorial of n isn(n-1)(n-2)...21 int n,f=1; cin >> n; for (i=2; i<=n; i++) { f *= i; } cout << “The factorial of ” << n << “ is ” << f << “.”; IT 279, Chung-Chih Li

  41. Compare: for and while for(Initialization_action; Condition; Condition_update) { statement_list; } for(Initialization_action; Condition; Condition_update) { statement_list; } 1 2 3 int n,f=1; cin >> n; for (i=2; i<=n; i++) { f *= i; } cout << “The factorial of ” << n << “ is ” << f << “.”; i=2; while (i<=n) { f *= i; i++; } IT 279, Chung-Chih Li

  42. For Loop is not really a definite loop int n,i; n = 100; for (i=1; i <= n; i++) { statement_list; } int n,i; n = 100; i = 1; while (i <= n) { statement_list; i++; } v.s. IT 279, Chung-Chih Li

  43. break and continue The break statement in a for/whileloop will force the program to jump out of the for/while loop immediately. The continue statement in a for/whileloop will force the program to update the loop condition and then check the condition immediately. for(Initialization_action;Condition;Condition_update) { statement_list; } IT 279, Chung-Chih Li

  44. b Nested loops (loop in loop) ************* ************* ************* ************* cin >> a >> b; for (int i = 0; i < a; i++) { for (int j=0; j<b; j++) { cout << “*”; } cout << endl; } a IT 279, Chung-Chih Li

  45. b Nested loops (2) * ** *** **** a int a,b; cin >> a >> b; for (int i = 0; i < a; i++) { for (int j=0; j<b; j++) { if (j > i) break; cout << “*”; } cout << endl; } IT 279, Chung-Chih Li

  46. b Nested loops (3) * ** *** **** a int a,b; cin >> a >> b; for (int i = 0; i < a; i++) { for (int j=0; j<b && j < i; j++) { cout << “*”; } cout << endl; } if (j > i) break; j <= i; IT 279, Chung-Chih Li

  47. b Nested loops (4) ************* ************ *********** ********** a int a,b; cin >> a >> b; for (int i = 0; i < a; i++) { for (int j=0; j<b; j++) { if (j < i) cout << “ ”; else cout << “*”; } cout << endl; } = IT 279, Chung-Chih Li

  48. * *** ***** ******* ********* *********** Nested loops (5) int a,i,j; cin >> a; for (i = 0; i < a; i++) { for (j=0; j<a; j++) { if (j < a-i) cout << " "; else cout << "*"; } for (j=0; j<a; j++) { if (j > i) break; cout << "*"; } cout << endl; } IT 279, Chung-Chih Li

More Related