Logical Operators

Conditional OR

🙋 Need help? Ask an expert now!

Contrary to the "AND" operator, the "OR" operator is used in a boolean expression to check if at least one of expressions are true.The "OR" operator is also a logical operator because it combines two true/false values into a single true/false value. Let's take a look at the functionality of the "OR" operator.

ConditionalOrExample.java
package exlcode;

public class ConditionalOrExample {

  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) {
    // returns true if either one of the boolean values are 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");
    }
  }
}

The "OR" operator only checks to see if one the boolean values are true. Here are the possibilities and the results for each case.

  1. true || true = true
  2. true || false = true
  3. false || true = true
  4. false || false = false

The "OR" operator functions similarly to how the word "or" is used in English. If one of the options you mention when using "or" is met, the other one doesn't matter. For example, if you are looking for a muffin or a donut, your needs are satisfied when you find a muffin, a donut, or both.

Application Question

Consider the following code segment:

public static void exampleMethod(int paramOne, int paramTwo)
            {
              if ((paramOne > 0) && (paramTwo > 0)) {
                if (paramOne > paramTwo){
                  System.out.println("A");
                }
                else{
                  System.out.println("B");
                }
              }
              else if ((paramTwo < 0) || (paramOne < 0)) {
                System.out.println("C");
              }
              else if (paramTwo < 0) {
                System.out.println("D");
              }
              else { 
                System.out.println("E");
              }
            }
            

What is printed as a result of executing exampleMethod(3,-2)?