Logical Operators Tutorial: && (and), || (or)

In Java, the && and the || short-circuit operators compare two or more logical expressions and return either true or false. This tutorial will cover the conditional and operator (&&), and the conditional or (||) operator. The easiest way to explain how these two operators function is to go over a quick refresher on the if statement.

The if statement structure

The most basic of the control flow statements is the if statement. The if statement evaluates a condition to determine if the result is either true or false. The conditional test is contained inside the set of parenthesis located directly after the Java keyword if. If the condition evaluates to be true then the statement(s) inside the code block are executed. The code block braces are optional for execution of just a single statement.

if (condition) {
     Statements are executed if condition equals true.
}


The conditional test can be performed using one of the Java relational operators such as:
== equal to
> greater than
>= greater than or equal to
< less than
<= less than or equal to
!= not equal to

The Short-circuit && (and) Operator

What if we want change the flow of a program based on the results of more than just one control statement? We could nest some if statements like this:
int a=5, b=6, c=7;
if (c>b) {
    if (b>a) {
          System.out.println("c is greater than a and b");
     }
}

There is a much better and cleaner looking way to do the exact same thing - the && operator. The && operator is placed between two or more conditional expressions and will check the result of each expression from left to right. As long as the conditional expressions keep resulting to true, further conditional expressions will continue to be evaluated. If we make it to the end of the conditional expressions and all of them have evaluated to true, then the total result will return true and the code inside the code block will execute.
int a=5, b=6, c=7;
if ((c>b) && (b>a)) {
//enclosing the (conditional expressions) in parenthesis is optional, but it makes it more readable in my opinion
     System.out.println("c is greater than a and b");
}

int a=5, b=6, c=7, d=8, e=9;
if ((e>d) && (d>c) && (c>b) && (b>a)) {
     System.out.println("e is greater than a, b, c, and d");
}

So why is the && called the short-circuit and operator? The short-circuit comes into play when one of the conditional expressions evaluate to false. Any remaining conditional expressions will not be evaluated and the total result will return false.
int a=5, b=6, c=7, d=8, e=9;
if ((e==d) && (d>c) && (c>b) && (b>a)) {
     System.out.println("This will never print");
     System.out.println("(d>c) && (c>b) && (b>a) were not evaluated");
}

In the code above, when java evaluates the conditional expression "is e equal to d(e==d)?", the result is false. At that point the && operator will "short-circuit" out and any remaining expressions will not be evaluated. The result will be false.

The Short-circuit || (or) Operator

The || operator is placed between two or more conditional expressions and will check the result of each expression from left to right. As long as the conditional expressions keep resulting to false, further conditional expressions will continue to be evaluated. If we make it to the end of the conditional expressions and all of them have evaluated to false, then the total result will return false. However, if just a single conditional expression evaluates to true, the || operator will "short-circuit" out and any remaining expressions will not be evaluated. The result will be true.
int currentTempFarenheit = 75;
int maxComfort = 85;
int minComfort = 54;
if ((currentTempFarenheit <= maxComfort) || (currentTempFarenheit >= minComfort++)) {
     System.out.println("The temperature outside is nice.");
     System.out.println("(currentTempFarenheit >= minComfort++) was not evaluated");
     System.out.println("minComfort is still equal to "+minComfort);
} else {
     System.out.println("The temperature outside is uncomfortable.");
}



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md ShortCircuitAndOr
C:\Java>cd ShortCircuitAndOr
C:\Java\ShortCircuitAndOr>Notepad ShortCircuitAndOr.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class ShortCircuitAndOr {
    public static void main(String args[]) {
        int a=5, b=6, c=7, d=8, e=9;
        if ((c>b) && (b>a)) {
            System.out.println("c is greater than a and b");
        }
        System.out.println();

        if ((e>d) && (d>c) && (c>b) && (b>a)) { 
            System.out.println("e is greater than a, b, c, and d");
        }
        System.out.println();

        if ((e==d) && (d>c) && (c>b) && (b>a)) { 
            System.out.println("This will never print");
        } else {
            System.out.println("(d>c) && (c>b) && (b>a) were not evaluated");
        }
        System.out.println();

        int currentTempFarenheit = 75;
        int maxComfort = 85;
        int minComfort = 54;
        if ((currentTempFarenheit <= maxComfort) || (currentTempFarenheit >= minComfort++)) { 
            System.out.println("The temperature outside is nice.");
            System.out.println("(currentTempFarenheit >= minComfort++) was not evaluated");  
            System.out.println("minComfort is still equal to "+minComfort);          
        } else {
            System.out.println("The temperature outside is uncomfortable.");
        
        }
    }
}

Now switch back to the command prompt (CMD) and type in javac ShortCircuitAndOr.java and press Enter.
Now type in java ShortCircuitAndOr and press Enter.


C:\Java\ShortCircuitAndOr>javac ShortCircuitAndOr.java
C:\Java\ShortCircuitAndOr>java ShortCircuitAndOr
c is greater than a and b

e is greater than a, b, c, and d

(d>c) && (c>b) && (b>a) were not evaluated

The temperature outside is nice.
(currentTempFarenheit >= minComfort++) was not evaluated
minComfort is still equal to 54


Final thoughts

Be sure to check out my tutorial on the non short-circuit Logical Operators & (and), | (or) tutorial. You will need to know the subtle differences between the two versions. In the future, when you come across && and & or || or | you will understand how the flow of the program will play out.


Tutorials