Skip to main content

Learning about Code Structure

Section 1.6 Rule 1) Conjoining Conditions

Please watch the video to learn when to conjoin conditions with || and then answer the following questions.
Figure 1.6.1. Conjoining conditions with || part 1

Activity 1.6.1.

    Consider the following Java function. What happens when the function is called with the given inputs print(2, 0, 0);?
    public static void print(int x, int y, int z) {
        if (x > 10 || x / y > 10 || z > 10) {
            System.out.println("Condition met");
        }
        System.out.println("End of program");
    }
    
  • The code first prints "Condition met" and then prints "End of program"
  • Think about what you learned for short circuit evaluation from the video
  • The code only prints "End of program"
  • Think about what you learned for short circuit evaluation from the video
  • The code throws an error for division by zero
  • The first condition is false, therefore the program checks the second condition and throws an exception.

Activity 1.6.2.

    What happens when the function is called with these new inputs print(12, 0, 20);?
  • The code first prints "Condition met" and then prints "End of program"
  • Correct! The first condition is true, the program doesn’t bother checking the rest of the conjoined conditions and considers the whole as true.
  • The code only prints "End of program"
  • The body of the if statement has a print statement, so the execution continues and the last line will also be printed.
  • The code throws an error for division by zero
  • Due to short circuit evaluation when the first condition is true, the whole condition evaluates to true and the code doesn’t even check the rest of the conjoined conditions.
You have attempted 1 of 4 activities on this page.