Skip to the content.
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

Unit 3 Team Teach - 3.5 Compound Booleans

Unit 3 Team Teach

3.5 Compund Boolean expressions

Nested conditional statements

definition: if statements within other if statements

public class Main {
    public static void main(String[] args) {
        int age = 20;
        boolean isStudent = true;

        // Outer if-else block
        if (age >= 18) {
            // Nested if-else block inside the first condition
            if (isStudent) {
                System.out.println("You are an adult and a student.");
            } else {
                System.out.println("You are an adult.");
            }
        } else {
            System.out.println("You are not an adult.");
        }
    }
}

Simple Example of a Nested Conditional Statement

Let’s look at a very basic example of a nested conditional statement using if-else blocks.

Scenario:

We want to check if a person is an adult and, if they are, we want to know if they are also a student.

Compound Conditional Statement

A compound conditional statement is when two or more conditions are combined into a single if statement using logical operators like && (AND), || (OR), or ! (NOT). This allows us to check multiple conditions at once without needing to nest if statements.

Logical Operators:

  • && (AND): True if both conditions are true.
  • || (OR): True if at least one condition is true.
  • ! (NOT): Reverses the result of a condition (true becomes false, and false becomes true).

Example of a Compound Conditional Statement

Let’s say we want to check if a person is an adult and a student at the same time. Instead of using a nested if statement, we can use a compound conditional statement.

public class Main {
    public static void main(String[] args) {
        int age = 20;
        boolean isStudent = true;

        // Compound conditional using && (AND)
        if (age >= 18 && isStudent) {
            System.out.println("You are an adult and a student.");
        } else {
            System.out.println("Either you are not an adult, or you are not a student.");
        }
    }
}

common mistake: Dangling else

- Java does not care about indentation
- else always belongs to the CLOSEST if
- curly braces can be use to format else so it belongs to the FIRST 'if'

Popcorn hack

  • explain the purpose of this algorithm, and what each if condition is used for
  • what would be output if input is
    • age 20
    • anual income 1500
    • student status: yes

The purpose of this is to check the potential memberships or discounts for a person

For age 20, income 1500, and student…

  • Not basic membership
  • Not premium membership
  • Student Discount
  • Not Senior Discount
  • Not You do not qualify for any memberships or discounts.
function checkMembershipEligibility() {
    // Get user input
    let age = parseInt(prompt("Enter your age:"));  // Age input
    let income = parseFloat(prompt("Enter your annual income:"));  // Annual income input
    let isStudent = prompt("Are you a student? (yes/no):").toLowerCase() === 'yes';  // Student status input

    // Initialize an empty array to hold results
    let results = [];

    // Check eligibility for different memberships

    // Basic Membership
    if (age >= 18 && income >= 20000) {
        results.push("You qualify for Basic Membership.");
    }

    // Premium Membership
    if (age >= 25 && income >= 50000) {
        results.push("You qualify for Premium Membership.");
    }

    // Student Discount
    if (isStudent) {
        results.push("You are eligible for a Student Discount.");
    }

    // Senior Discount
    if (age >= 65) {
        results.push("You qualify for a Senior Discount.");
    }

    // If no eligibility, provide a default message
    if (results.length === 0) {
        results.push("You do not qualify for any memberships or discounts.");
    }

    // Output all results
    results.forEach(result => console.log(result));
}

// Call the function to execute
checkMembershipEligibility();

Popcorn Hack #2

  • Write a program that checks if a person can get a discount based on their age and student status. You can define your own discount criteria! Use compound conditionals to determine the output.
public class Main {
    public static void main(String[] args) {
        int age = 30; // Change this value for testing
        boolean isStudent = true; // Change this value for testing
        if (age >= 65) {
            System.out.println("Senior Discount");
        }

        else if (age < 18) {
            System.out.println("Minor Discount");
        }
    
    
        if (isStudent) {
            System.out.println("Student Discount");
        }

    }
}