What if you want two things to be true before the body of the conditional is executed? Use && as a logical and to join two Boolean expressions and the body of the condition will only be executed if both are true.
Coding Exercise
What if you want to go out and your parents say you can go out if you clean your room and do your homework? Run the code below and try different values for cleanedRoom and didHomework and see what they have to be for it to print You can go out .
What if it is okay if only one of two things is true? Use || as a logical or to join two Boolean expressions and the body of the condition will be executed if one or both are true.
Coding Exercise
For example, your parents might say you can go out if you can walk or they don’t need the car. Try different values for walking and carIsAvailable and see what the values have to be to print You can go out .
In English, we often use an exclusive-or like in the sentence “do you want to be player 1 or player 2?” where you can’t be both player 1 and player 2. In programming, the or-operator is an inclusive-or which means that the whole expression is true if either one or the other or both conditions are true.
With numerical values, the or-operator is often used to check for error conditions on different ends of the number line, while the and-operator is often used to see if a number is in an range.
Coding Exercise
Explore how && and || are used with numbers below. Try different values for score like -10 and 110 in the code below.
The not (!) operator can be used to negate a boolean value. We’ve seen ! before in != (not equal). In Java, ! has precedence (is executed before) &&, and && has precedence over ||. Parentheses can be used to force the order of execution in a different way. If you mix ! with && and ||, be careful because the results are often the opposite of what you think. We’ll see examples of this in the next lesson.
Coding Exercise
The code below says if homework is not done, you can’t go out. Try different values for homeworkDone .
In Java, ! will be executed before &&, and && will be executed before ||, unless there are parentheses. Anything inside parentheses is executed first.
The following table (also called a truth table) shows the result for P && Q when P and Q are both expressions that can be true or false. An expression involving logical operators like (P && Q) evaluates to a Boolean value, true or false. As you can see below the result of P && Q is only true if both P and Q are true.
Check your understanding
The following table shows the result for P || Q when P and Q are both expressions that can be true or false. As you can see below the result of P || Q is true if either P or Q is true. It is also true when both of them are true.
Check your understanding
Check your understanding
3-5-7: What is printed when the following code executes and x has been set to 3 and y has been set to 9?
1if (x > 0 && (y / x) == 3) 2 3 System.out.println("first case"); 4> 5else 6 7 System.out.println("second case"); 8>
3-5-8: What is printed when the following code executes and x has been set to 3 and y has been set to 6?
1if (x > 0 && (y / x) == 3) 2 3 System.out.println("first case"); 4> 5else 6 7 System.out.println("second case"); 8>
3-5-9: What is printed when the following code executes and x has been set to 3 and y has been set to 6? Notice that it is now an or (||) instead of and.
1if (x > 0 || (y / x) == 3) 2 3 System.out.println("first case"); 4> 5else 6 7 System.out.println("second case"); 8>
Both && and || use short circuit evaluation. That means that the second condition isn’t necessarily checked if the result from the first condition is enough to tell if the result is true or false. In a complex conditional with a logical and ( && ) both conditions must be true, so if the first is false, then the second doesn’t have to be evaluated. If the complex conditional uses a logical or ( || ) and the first condition is true, then the second condition won’t be executed, since only one of the conditions needs to be true.
In a complex conditional using a logical and ( && ) the evaluation will short circuit (not execute the second condition) if the first condition is false. In a complex conditional using a logical or ( || ) the evaluation will short circuit if the first condition is true.
Check your understanding
3-5-10: What is printed when the following code executes and x has been set to 0 and y to 3?
1if (x > 0 && (y / x) == 3) 2 3 System.out.println("first case"); 4> 5else 6 7 System.out.println("second case"); 8>
3-5-11: What is printed when the following code executes and x has been set to zero and y is set to 3?
1if (x == 0 || (y / x) == 3) 2 3 System.out.println("first case"); 4> 5else 6 7 System.out.println("second case"); 8>
3-5-12: What is printed when the following code executes and x has been set to zero and y is set to 3?
1if ((y / x) == 3 || x == 0 ) 2 3 System.out.println("first case"); 4> 5else 6 7 System.out.println("second case"); 8>
Explore the following problems:
© Copyright 2015 Barb Ericson, 2019 revised by Beryl Hoffman Mobile CSP, 2020 revised by Linda Seiter and Dan Palmer. Created using Runestone 7.3.7.