Skip to main content

Learning about Code Structure

Section 1.7 Rule 1) Conjoining Conditions

Continue watching the video to learn when to conjoin conditions with || and answer the following questions.
Figure 1.7.1. Conjoining conditions with || part 2

Activity 1.7.1.

    Which of the following options summarize the main points in the video?
    Select all that apply.
  • To refactor sequential if statements, when they have the same body, we can always conjoin their conditions using ||.
  • No! Not always
  • We can conjoin the conditions of sequential if statements with the same body using || when those conditions are exclusive.
  • Correct!
  • Sequential if statements with mutually exclusive conditions, can be refactored to use else if structure and when their body is also the same, they can be conjoined using ||.
  • Correct!
  • if we have sequential if statements where each statement prints a message, and conditions are NOT mutually exclusive, multiple if conditions may evaluate to true in one run.
  • Correct!

Activity 1.7.2.

    Which of the following options indicates sequential if statements with exclusive conditions?
    Select all that apply.
  • if (x > 0) {
    	System.out.println("Positive");
    }
    if (x % 2 == 0) {
    	System.out.println("Even");
    }
    
  • No they are not exclusive! for any even number > 0 both conditions evaluate to ture and both messages will be printed. For example for x == 10.
  • if (x >= 10) {
    	System.out.println("High");
    }
    if (x >= 0 && x < 10) {
    	System.out.println("Middle");
    }
    if (x < 0) {
    	System.out.println("Low");
    }
    
  • Correct! At a single run, x can only be in one of three ranges: either greater than 10, between 0 and 10 (inclusive) or less than 0.
  • if (x % 2 == 1) {
    	System.out.println("Odd");
    }
    if (x % 2 == 0) {
    	System.out.println("Even");
    }
    
  • Correct! x can NOT be both odd and even in a single run.
  • if (x > 0) {
    	System.out.println("X is positive");
    }
    if (y <= 0) {
    	System.out.println("Y is not positive");
    }
    
  • One condition checks the value of x and the other checks y and for (x == 10 and y == -2) both conditions can evaluate to true.

Checkpoint 1.7.2.

We want the canPlayOutside() function to take a String parameter, weather, and print some messages. If the weather equals "sunny" the function prints, "You can play outside!". Similarly, when the weather is "cloudy" the function prints "You can play outside!". However, if the weather is "windy", it prints, "you should play inside!". To compare two String values, use equals() method.
public static void canPlayOutside(String weather) {
	if (.........blank1...............) {
           System.out.println(You can play outside!);
    } .......blank2....... {
           System.out.println(You should play inside!);
    }
 }
Put your responses here:
Blank 1:
Blank 2:
You have attempted 1 of 5 activities on this page.