Selection Structures Tonga Institute of Higher Education
Introduction Programs must handle different conditions when the program is running. Selection structures allow programs to handle different conditions. If…Else Statement Switch Statement
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
Selection Structures/Statements Selection structures allow programs to run different code based on runtime conditions. If…Else Statement Switch Statement
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!”
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
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.
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.
Nested IF Statements Check for different conditions.
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: >=
Comprehension Check If Statements
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!”
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”))
Comprehension Check If Statements with String Comparisons
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)
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
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
Logical Operators - NOT ! Operates on a single expression Returns the opposite of the expression. ConditionOperatorResult TrueNotFalse NotTrue
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”)) = ?
Comprehension Check If Statements with Boolean Logic
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