180 likes | 186 Views
Learn the syntax and examples of if-else statements in programming. Explore usage scenarios and examples with nested if statements. Grasp how to avoid the "Dangling Else" problem and understand short-circuit evaluation.
E N D
Programming If Statements & Relational Operators, Part 2
The if-else Statement • Syntax if(condition) Action_AelseAction_B • if the condition is true then execute Action_Aelse execute Action_B • Example: if(value == 0) cout << "value is 0"; else cout << "value is not 0"; condition false true Action_A Action_B
Choice (if and else) if <it's sunny>{ <go to beach> } else{ <take umbrella> }
Finding the Big One int value1; int value2; int larger; cout << "Enter two integers: "; cin >> value1 >> value2; if(value1 > value2) larger = value1; else larger = value2; cout << "Larger of inputs is: " << larger << endl;
Finding the Big One const doublePI = 3.1415926; intradius; doublearea; cout << "Enter the radius of the circle: "; cin >> radius; if(radius > 0){ area = radius * radius * PI; cout << "The area of the circle is: " << area; } else cout << "The radius has to be positive " << endl;
Even or Odd int value1; bool even; cout << "Enter a integer: "; cin >> value; if(value%2 == 0) even = true; else even = false; // even = !(value%2);
if-else-if Statements if <condition 1 exists>{ <do Q> } else if <condition 2 exists>{ <do R> } else if <condition 3 exists>{ <do S> } else{ <do T> } Q R S T
if-else-if Statements if <1PM or 7PM>{ <eat> } else if <Mon, Wed or Fri>{ <goto COMP 102> } else if <Tues or Thurs AM>{ <goto MATH 113> } else{ <sleep> }
if-else-if Statement int people, apples, difference; cout << "How many people do you have?\n"; cin >> people; cout << "How many apples do you have?\n"; cin >> apples; if(apples == people) cout << "Everybody gets one apple.\n"; else if(apples > people){ difference = apples - people; cout << "Everybody gets one apple, & there are " << difference << " extra apples.\n";} else{ difference = people - apples; cout << "Buy " << difference << " more apples so that everyone gets one apple.\n";}
if-else-ifExample int score; cout << "Please enter a score: "; cin >> score; if (score >= 90) cout << "Grade = A" << endl; else if (score >= 80) cout << "Grade = B" << endl; else if (score >= 70) cout << "Grade = C" << endl; else if (score >= 60) cout << "Grade = D" << endl; else// totalscore < 59 cout << "Grade = F" << endl;
Nested if Statements • Nested means that one complete statement is inside another if <condition 1 exists>{ if <condition 2 exists>{ if <condition 3 exists>{ <do A> } <do B> } <do C: sleep> }
Nested if Statements • Example: if <it's Monday>{ <go to HKUST> if <it's time for class>{ if <it's raining>{ <bring umbrella> } <go to COMP 102> } }
Nested if Statements • Consider the following example: if the customer is a member, then { If the customer is under 18, then the entrance fee is half the full fee. If the customer is 18 or older, then the entrance fee is 80% of the full fee. } The if statements deciding whether to charge half fee to someone under 18 or whether to charge 80% to someone over 18 are only executed if the outer if statement is true, i.e. the customer is a member. Non-members, no matter what their age, are charged full fee.
Nested if Statements • Consider a variant of the previous example: if the customer is a member, then { If the customer is under 18, then the entrance fee is half the full fee. } If the customer is 18 or older, then the entrance fee is 80% of the full fee. Here, member customers under 18 will be charged half fee and all other customers over 18 will be charged 80% of the full fee.
Nested if Statements If (member) { if (age < 18) { fee = fee * 0.5; } } if (age >=18) fee = fee * 0.8; If (member) { if (age < 18) { fee = fee * 0.5; } if (age >=18) fee = fee * 0.8; }
“Dangling Else” Problem • Always pair an else with the most recent unpaired if in the current block. Use extra brackets { } to clarify the intended meaning, even if not necessary. For example, what is the value of c in the following code? int a = -1, b = 1, c = 1; if( a > 0 ) if( b > 0 ) c = 2; else c = 3;
“Dangling Else” Problem (A)int a = -1, b = 1, c = 1; if (a > 0) { if (b > 0) c = 2; else c = 3; } (B) int a = -1, b = 1, c = 1; if (a > 0) { if (b > 0) c = 2; } else c = 3; (A) is the correct interpretation. To enforce (B), braces have to be explicitly used, as above.
Short-circuit Evaluation • If the first operand of a logical and expression is false, the second operand is not evaluated because the result must be false. • If the first operand of a logical or expression is true, the second operand is not evaluated because the result must be true.