Presentation is loading. Please wait.

Presentation is loading. Please wait.

Decision Making Selection structures (if....else and switch/case)

Similar presentations


Presentation on theme: "Decision Making Selection structures (if....else and switch/case)"— Presentation transcript:

1 Decision Making Selection structures (if....else and switch/case)

2 if....else We use selection many times every day as we make decisions For Example: if the car is available drive to work Q: what will happen if the car is not available? A: In this case, an alternate action should be stated. Condition Action

3 if....else if the car is available drive to work else take public transit to work The word else indicates the "take public transit" is the alternate action that will take place if the condition is NOT true. Only one condition can be true.

4 else if The words else and if are used together to introduce another possible condition Many conditions can be included but only one condition will be true. if the car is available drive to work else if public transit is running take public transit to work else if the bike is available bike to work else walk to work

5 A Condition (boolean expression) can be formed by using logical operator Logical Operators consist of: == Equals > Greater than != Not Equals < Less than <= Less than or equals >= Greater than or equals && And || Or (the ‘pipe’ symbols)

6 And (&&) Result is true if All Conditions are true For example: If you are hungry, AND you have money, THEN you can buy lunch

7 && (And) Expression1Expression2Result True False True False True False

8 || (OR) Result is true if ANY condition is true. For example: If you have money OR you have a credit card, you can pay the bill.

9 || (Or) Expression1Expression2Result True False True False True False

10 != (Not) Changes True Result to false & Vice Versa. For Example: If not sunny then it is cloudy.

11 != (Not ) ExpressionResult True False True

12 Selection structure in Java Set brackets required if more than 1 statement only And... both must be true Default... ‘else’ occurs if no other condition prior is true Default Selection: if ( mark >=50 ) { c.println ( “You passed” ); } else { c.println ( “You failed”); }

13 Multiple Selection

14 if without else In some cases, there is an action to carry if a condition is true and nothing to do if the condition is not met. If a discount of 25% is applied to all purchases over $200.00, the calculation would be: double discount=0; if (purchase>200.00) { discount=purchase*.25; purchase-=discount; }

15 "switch/case" Multiple - Selection Statement Java provides the switch/case multi- selection statements to perform different actions based on the possible values of an integer variable or expression. Each action is associated with a constant integral value (i.e. int, char, short or byte) that the variable or expression can have. It cannot be used with long or double.

16 This structure is used in conjunction with the break instruction. This type of structure is often referred to in other languages as the select …case structure.

17 Switch: Java Syntax switch ( identifier) { case value1: // …statements to be executed break ; case value2: // …statements to be executed break ; case value3: case value4: case value5: // …statements to be executed… break ; default : // …statement break ; } // switch

18 Switch: Integer Example // Here is an example of a switch construct that outputs a comment based on a // student's mark out of 10 int mark = In.getInt ( ); switch (mark) { case 10: System.out.println ("Perfect."); break; case 9: case 8: System.out.println ("Pretty Good."); break; Case 7: case 6: System.out.println ("Okay."); break; case 5: System.out.println ("Squeaked By!"); break; default: System.out.println ("Did not pass. Ouch!"); break; } // switch

19 Notes The keywords if and else must be lower case A line that contains a key word does not end with a semi- colon. The condition is in parentheses (age < 13) By convention the action statements should be indented Each set of actions is contained in braces, the braces may be omitted if there is only one action but it is good practice to include them. The equality operator in Java is two equals signs. If you are testing to see if a person's age is 21, the statement would be: if (age==21) The switch/case construct must be used in conjunction with the break statement. The default statement is to be used as a catch all case in case none of the above cases is executed.

20 Homework Page 109 # 1 Page 115 # 4 Page 118 # 3 Read page 98, 99 (compareTo)


Download ppt "Decision Making Selection structures (if....else and switch/case)"

Similar presentations


Ads by Google