220 likes | 356 Views
branch. #include <iostream> using namespace std; void main() { int a, b; cin >> a >> b; if (b == 0) { cout << “ You will see an error.<br> †; } cout << “ a divided by b is †<< a/b; cout << endl; }. max II. #include <iostream> using namespace std; void main() {
E N D
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; }
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;
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 ) { 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.
Operators • Arithmetic operators: • + - / * % • Relational operators: • == > < <= >= != • Logical operators: • || && !
== > < <= >= != 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
|| && ! 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
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
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
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.
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!!"; } }
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”;
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”;
Ambiguity in English I saw the girl with a telescope. I saw the girl with her boy friend.
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!!”;
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”;
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; }
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.
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; =
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; }
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; }
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;