1 / 22

branch

branch. #include &lt;iostream&gt; using namespace std; void main() { int a, b; cin &gt;&gt; a &gt;&gt; b; if (b == 0) { cout &lt;&lt; “ You will see an error.<br> ” ; } cout &lt;&lt; “ a divided by b is ” &lt;&lt; a/b; cout &lt;&lt; endl; }. max II. #include &lt;iostream&gt; using namespace std; void main() {

raven
Download Presentation

branch

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. branch #include <iostream> using namespace std; void main() { int a, b; cin >> a >> b; if (b == 0) { cout << “You will see an error.\n”; } cout << “a divided by b is” << a/b; cout << endl; }

  2. 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;

  3. Branching Condition Statement list 1 T Condition Statement list T F F Statement list 2

  4. 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.

  5. Operators • Arithmetic operators: • + - / * % • Relational operators: • == > < <= >= != • Logical operators: • || && !

  6. == > < <= >= != 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

  7. || && ! 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

  8. 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

  9. Example of if/else statement string ans,sname =“”; bool married; cout << “Are you married? (input Y or N):”; cin >> ans; if (ans == “Y” || ans == “y”) married = true; else married = false; if (married) { cout << “Input your spouse name:”; cin >> sname; } cout << “Welcome!!” << sname << “.”; else

  10. 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.

  11. 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!!"; } }

  12. 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 << “Tt is a week day”; else cout << d1 << “It is a weekend”;

  13. 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”;

  14. Ambiguity in English I saw the girl with a telescope. I saw the girl with her boy friend.

  15. 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!!”;

  16. 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”;

  17. 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; }

  18. 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.

  19. 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; =

  20. 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; }

  21. 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; }

  22. 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;

More Related