Once you click post assignment, learners in the selected class will immediately be able to see and complete this assignment
Back to Classroom
Conditional AND
Java Selection Statements Course
Engineers From These Top Companies and Universities Trust EXLskills
1M+ Professionals | 100+ Institutions
This is the EXLskills free and open-source Java Selection Statements Micro Course. It guides learners via explanation, demonstration, and thorough practice, from no more than a basic understanding of Java, to a moderate level of understanding regarding Java selection statements.
Is this course FREE?
Yes, this a 100% free course that you can contribute to on GitHub here!
Let's explore the conditional "AND" operator, which allows you to check whether or not two values are both true or both false before executing a statement. If you have experience with a truth table, you will see similarities here. The "AND" operator is a logical operator that turns two true/false values into a single true or false value. Look below to see how this operator functions.
ConditionalAndExample.java
package exlcode;
public class ConditionalAndExample {
public static boolean exampleVariableOne = true;
public static boolean exampleVariableTwo = true;
public static boolean exampleVariableThree = false;
public static boolean exampleVariableFour = false;
public static void main(String[] args) {
// only the boolean expression "true && true" returns true
if (exampleVariableOne && exampleVariableTwo) {
System.out.println("The boolean expression is true");
} else {
System.out.println("The boolean expression is false");
}
if (exampleVariableOne && exampleVariableThree) {
System.out.println("The boolean expression is true");
} else {
System.out.println("The boolean expression is false");
}
if (exampleVariableThree && exampleVariableFour) {
System.out.println("The boolean expression is true");
} else {
System.out.println("The boolean expression is false");
}
}
}
Only the expressions with true on both sides of the "AND" evaluate as true. Here are the different possibilities and their outcomes:
true && true = true
false && true = false
true && false = false
false && false = false
The AND operator evaluates both operands, the expressions on each side, before determining the final value. This is useful when you want two or more expression to be true before executing a statement. Using them in an if statement will simplify your code structure and reduce your time spent on coding.