Skip to main content

Learning about Code Structure

Section 1.8 Rule 1) Conjoining Conditions

Continue watching the video to learn when to conjoin conditions with ||, then answer the following questions.
Figure 1.8.1. Conjoining conditions with || part 3

Activity 1.8.1.

    Consider the following two functions (guessGame1() and guessGame2()). For which input(s) do these functions show different behavior?
    public static void guessGame1(int number) { 
       if (number % 4 == 0) {
          System.out.println("Good guess!");
       }  
       if (number % 7 == 0) {
          System.out.println("Good guess!");
       } 
    }
    
    public static void guessGame2(int number) { 
       if (number % 4 == 0 || number % 7 == 0) {
          System.out.println("Good guess!");
       } 
    }
    
  • 28
  • Correct! guessGame1(28) prints "Good guess" twice but guessGame2(28) prints "Good guess" only once!
  • 16
  • Not Correct! Both guessGame1(16) and guessGame2(16) print "Good guess" once.
  • 56
  • Correct! guessGame1(56) prints "Good guess" twice but guessGame2(56) prints "Good guess" only once.
  • 23
  • Not Correct! Neither guessGame1(23) nor guessGame2(23) prints anything. So for this input both functions have the same bahavior.

Activity 1.8.2.

    Which of the following options summarize the main points in the video?
    Select all that apply.
  • We can conjoin the conditions of sequential if statements with the same body containing a return statement using ||.
  • Correct! If the body of if statements involve return, break or continue statements, regardless of whether conditions are exclusive or not, we can conjoin conditions.
  • The functionality and execution of sequential if statements that contain return statements in their bodies are the same as if-else if structure.
  • Correct!
  • We can always conjoin the conditions of sequential if statements with the same body that includes print statements using ||.
  • Not always! For example, in the previous activity, when number == 28, conjoining the conditions results in "Good guess" being printed only once, whereas keeping them separate causes "Good guess" to be printed twice.
You have attempted 1 of 4 activities on this page.