CSA Blog

Ian Wu

Unit 3 Team Teach - 3.4

3.1: Boolean Expressions 3.2: If Control Flow 3.3: If Else 3.4: Else If 3.5: Compound Booleans 3.6: Equivalent Booleans 3.7: Comparing Objects 3.8: Homework

3.4 Else If Statements

Else If Statements: Used when you have multiple conditions that need to be checked sequentially.

Flow of Execution: Each condition is evaluated in the order written. The first true condition’s code runs, and the rest are skipped.

Structure:

  • Start with a single if statement.
  • Follow with as many else if statements as needed.
  • Optionally end with one else to handle any remaining cases. Key Concept: The order of conditions matters. More specific conditions should come before broader ones to ensure accurate results.

image

  1. If I was 19 what would it print out?
    • “Current age: 19”
    • “You can register to vote.”
    • “You are old enough for a license to drive.”
  2. If I was 13 what would it print out?
    • “Current age: 13”
  3. Create your if statement with one else if condition.
boolean a = true;
boolean b = false;
boolean c = false;

if (!(a ^ b)) {
    System.out.println("d");
} else if (c || b ^ a) {
    System.out.println("e");
} else {
    System.out.println("f");
}
e

Unit 3 Team Teach - 3.3

3.1: Boolean Expressions 3.2: If Control Flow 3.3: If Else 3.4: Else If 3.5: Compound Booleans 3.6: Equivalent Booleans 3.7: Comparing Objects 3.8: Homework

3.3 If Else Statements

Purpose of Else Statements

Else statements: Handles what happens when the if condition is false. Structure of If-Else:

  • If statement with a condition.
  • Else statement without a condition.
  • Both parts have code blocks surrounded by {}.

don’t forget the brackets

if (x > 10) {

        console.log("x is greater than 10");
        console.log("This code when the condition is true");
    } else {
    
        console.log("x is 10 or less");
        console.log("This code runs when the condition is false");
    }
    Without brackets:
    
    if (x > 10)
    
        console.log("x is greater than 10");
        console.log("this code will always run");
|           console.log("x is greater than 10");

illegal character: '\u00a0'



|           console.log("x is greater than 10");

illegal character: '\u00a0'



|           console.log("x is greater than 10");

illegal character: '\u00a0'



|           console.log("x is greater than 10");

illegal character: '\u00a0'



|           console.log("This code when the condition is true");

illegal character: '\u00a0'



|           console.log("This code when the condition is true");

illegal character: '\u00a0'



|           console.log("This code when the condition is true");

illegal character: '\u00a0'



|           console.log("This code when the condition is true");

illegal character: '\u00a0'



|           console.log("x is 10 or less");

illegal character: '\u00a0'



|           console.log("x is 10 or less");

illegal character: '\u00a0'



|           console.log("x is 10 or less");

illegal character: '\u00a0'



|           console.log("x is 10 or less");

illegal character: '\u00a0'



|           console.log("This code runs when the condition is false");

illegal character: '\u00a0'



|           console.log("This code runs when the condition is false");

illegal character: '\u00a0'



|           console.log("This code runs when the condition is false");

illegal character: '\u00a0'



|           console.log("This code runs when the condition is false");

illegal character: '\u00a0'

image

  1. Based on this code, if you were younger than 16 what would it print out?
    • “You are not old enough for a license yet”
  2. Write your own if else statement
boolean a = true;
boolean b = false;
boolean c = false;

if (a ^ b) {
    System.out.println("d");
} else {
    System.out.println("e");
}
d