1 / 18

Programming

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.

Download Presentation

Programming

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. Programming If Statements & Relational Operators, Part 2

  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

  3. Choice (if and else) if <it's sunny>{ <go to beach> } else{ <take umbrella> }

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

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

  6. 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);

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

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

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

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

  11. 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> }

  12. 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> } }

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

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

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

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

  17. “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.

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

More Related