Conditional AND

Logical Operators

Conditional AND

🙋 Need help? Ask an expert now!

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:

  1. true && true = true
  2. false && true = false
  3. true && false = false
  4. 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.

Edit Me on GitHub!

Application Question

#!exl::repl('index.prob.repl.yaml')