This book is now obsolete Please use CSAwesome instead.
5.3. Complex Conditionals¶
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 only if both are true. For example, what if you want to go out and your mom said you could 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. For example, your Dad might say you can go out if you can walk or he doesn’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
.
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. As you can see below the result of P && Q is only true if both P and Q are true.
P |
Q |
P && Q |
---|---|---|
true |
true |
true |
false |
true |
false |
true |
false |
? |
false |
false |
false |
5-3-3: The truth table above is missing one result. What is the result of P && Q when P=true
and Q=false
?
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.
P |
Q |
P || Q |
---|---|---|
true |
true |
true |
false |
true |
? |
true |
false |
true |
false |
false |
false |
5-3-4: The truth table above is missing one result. What is the result of P || Q
when P=false
and Q=true
?
Check your understanding
- first case
- This will only print if x is greater than 0 and it is not.
- second case
- This will print if x is less than or equal to zero or if y divided by x is not equal to 3.
- You will get a error because you can't divide by zero.
- Since the first condition if false when x is equal to zero the second condition won't execute. Execution moves to the else.
5-3-5: What is printed when the following code executes and x has been set to zero?
if (x > 0 && (y / x) == 3) System.out.println("first case");
else System.out.println("second case");
- first case
- This will print if both of the conditions are true and they are.
- second case
- This will print either of the conditions are false.
5-3-6: What is printed when the following code executes and x has been set to 3 and y has been set to 9?
if (x > 0 && (y / x) == 3) System.out.println("first case");
else System.out.println("second case");
- first case
- This will print if both of the conditions are true, but the second is not.
- second case
- This will print if either of the conditions are false and the second one is (6 / 3 == 2).
5-3-7: What is printed when the following code executes and x has been set to 3 and y has been set to 6?
if (x > 0 && (y / x) == 3) System.out.println("first case");
else System.out.println("second case");
- first case
- This will print if either of the two conditions are true. The first isn't true but the second will cause an error.
- second case
- This will print if both of the conditions are false. But, an error will occur when testing the second condition.
- You will get a error because you can't divide by zero.
- The first condition will be false so the second one will be executed and lead to an error since you can't divide by zero.
5-3-8: What is printed when the following code executes and x has been set to zero? Notice that it is now a logical or rather than an and.
if (x > 0 || (y / x) == 3) System.out.println("first case");
else System.out.println("second case");