Presentation is loading. Please wait.

Presentation is loading. Please wait.

Selection Structures Tonga Institute of Higher Education.

Similar presentations


Presentation on theme: "Selection Structures Tonga Institute of Higher Education."— Presentation transcript:

1 Selection Structures Tonga Institute of Higher Education

2 Introduction Programs must handle different conditions when the program is running. Selection structures allow programs to handle different conditions.  If…Else Statement  Switch Statement

3 Source Code Execution Order Generally, source code is run in order But we don’t always run in order This is run first This is run second This is run third

4 Selection Structures/Statements Selection structures allow programs to run different code based on runtime conditions.  If…Else Statement  Switch Statement

5 If Statement / Structure This allows a program to run different code based on a condition. They require: 1. The condition to test for. 2. The code to run if the condition is true. 3. The code to run if the condition is false. (Optional) Example: 1. Condition: If my name is Dave Shin. 2. If True: Print “You are the teacher!” 3. If False: Print “You are a student!”

6 Example of an If Statement Example: 1. Condition: If my name is Dave Shin. 2. If True: Print “You are the teacher!” 3. If False: Print “You are a student!” Keywords Condition – Must be in parenthesis! Code to run if True Code to run if False

7 Examples: IF Statements - 1 If the condition is TRUE, then execute 1 line of code. If the condition is TRUE, then execute 1 line of code. If the condition is FALSE, then execute 1 line of code.

8 Examples: IF Statements - 2 If the condition is TRUE, then execute multiple lines of code. If the condition is FALSE, then execute multiple lines of code. If the condition is TRUE, then execute 1 line of code. If the condition is FALSE, then execute 1 line of code.

9 Nested IF Statements Check for different conditions.

10 Comparison Operators These work well with primitive data types These do not work well with objects The return of a comparison operator is a boolean (true/false) Equal: ==  Remember to use 2 equal signs! (Using only 1 assigns values) Not Equal: != Less Than: < Less Than or Equal To: <= Greater Than: > Greater Than or Equal To: >=

11 Comprehension Check If Statements

12 Comparing Objects - 1 If (x == y) Using == with objects works differently than expected.  Only compares memory addresses to see if they are referencing the same object. Normally, you don’t want this. Prints “Boo!”

13 Comparing Objects - 2 Normally, you want to compare the data inside of the objects. Use the Object.equals(Object anObject) method to compare the data inside of objects. Example: if (aString.equals(“hello”)) Example: if (name.equals(“Dave”))

14 Comprehension Check If Statements with String Comparisons

15 Logical Operators Logical operators return a true or false value as a result of performing an operation on two booleans. 1. AND - & or && 2. OR - | or || 3. NOT - ! Use these to evaluate multiple conditions. Example: We own a restaurant. We want to buy chicken and pigs from large suppliers. Our criteria for finding a supplier could be: (ChickenQuantity > 1000 && PigQuantity > 1000) (ChickenQuantity > 5000 || PigQuantity > 5000)

16 Logical Operators - AND & or &&  Both conditions must be true to return true. Otherwise, false is returned.  & - Always evaluates both conditions  && - Only evaluates second condition if necessary Condition 1OperatorCondition 2Result TrueAndTrue AndFalse AndTrueFalse AndFalse

17 Logical Operators - OR | or ||  At least one condition must be true to return true. Otherwise, false is returned.  I - Always evaluates both conditions  II - Only evaluates second condition if necessary Condition 1OperatorCondition 2Result TrueOrTrue OrFalseTrue FalseOrTrue FalseOrFalse

18 Logical Operators - NOT !  Operates on a single expression  Returns the opposite of the expression. ConditionOperatorResult TrueNotFalse NotTrue

19 Practice! String name1 = “Dave” String name2 = “Cathy” String name3 = “Jenny” 1. !(name1.equals(“Bob”) && name2.equals(“Cathy”) || name3.equals(“Jenny”)) = ? 2. !(!(name1.equals( “Dave”) || name2.equals(“Bob”))) = ? 3. !(name1.equals(“Dave”)) && !(name2.equals(“Bob”)) = ?

20 Comprehension Check If Statements with Boolean Logic

21 Switch Statement / Structure This allows a program to run different code based on a condition. Must evaluate to a Byte, Short, Int or Char data type


Download ppt "Selection Structures Tonga Institute of Higher Education."

Similar presentations


Ads by Google