1 / 15

Programming

Programming. If Statements & Relational Operators, Part 2. The if-else Statement. Syntax if ( condition ) Action_A else Action_B if the condition is true then execute Action_A else execute Action_B Example: if (value == 0) cout << "value is 0"; else

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

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

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

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

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

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

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

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

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

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

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