Skip to the content.
Boolean Expressions If Statements and Control Flow If-Else Statements Code Example Hacks Quiz

Unit 3 Team Teach - 3.2

Unit 3 Team Teach

3.2 If Statements and Control Flow

Screenshot 2024-09-16 at 8 07 34 AM Screenshot 2024-09-16 at 8 07 41 AM

popcorn hack

create test cases that do not satisy the condition above. you can copy/paste the code into the new code cell

public static void main(String[] args) {
    int myAge = 16;
    System.out.println("Current age: " + myAge);
    
    if (myAge >= 16) {
        System.out.println("You can start learning to drive!");
    }

    System.out.println("On your next birthday, you will be " + (myAge + 1) + " years old!");
}
public class MyProgram {
    public static void main(String[] args) {
        // Test case 1: Age less than 16
        int myAge1 = 15;
        System.out.println("Test Case 1 - Current age: " + myAge1);
        
        if (myAge1 >= 16) {
            System.out.println("You can start learning to drive!");
        }
    
        System.out.println("On your next birthday, you will be " + (myAge1 + 1) + " years old!");
        
        // Test case 2: Age less than 16
        int myAge2 = 14;
        System.out.println("Test Case 2 - Current age: " + myAge2);
        
        if (myAge2 >= 16) {
            System.out.println("You can start learning to drive!");
        }
    
        System.out.println("On your next birthday, you will be " + (myAge2 + 1) + " years old!");
    
        // Test case 3: Age less than 16
        int myAge3 = 10;
        System.out.println("Test Case 3 - Current age: " + myAge3);
        
        if (myAge3 >= 16) {
            System.out.println("You can start learning to drive!");
        }
    
        System.out.println("On your next birthday, you will be " + (myAge3 + 1) + " years old!");
    }
}

// Call the main method
MyProgram.main(null);
Test Case 1 - Current age: 15
On your next birthday, you will be 16 years old!
Test Case 2 - Current age: 14
On your next birthday, you will be 15 years old!
Test Case 3 - Current age: 10
On your next birthday, you will be 11 years old!

If statements can be used to create chatbots –> Magpie Lab Screenshot 2024-09-25 at 2 40 43 AM

Screenshot 2024-09-25 at 2 40 54 AM

  • the user’s input affects the flow of the program