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

In Java, the & (and) and the | (or) logical 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. This tutorial is going to be very similar to my short-circuit Logical Operators Tutorial: && (and), || (or) Tutorial.

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 & (and) Operator

What if we want change the flow of a program based on the results of more than just one expression? 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. If each and every one of the conditional expressions evaluate 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, sometimes doing so is more readable
     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");
}


Each expression will be evaluated and if one or more of the conditional expressions evaluate to false, then 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");
}


What is the difference between the & operator and the && operator? Good question, there is only one difference.
The & operator will evaluate each and every conditional expression.
The && operator will evaluate the expressions from left to right and if an expression evaluates to false, the total result will be false - any remaining expressions will not be evaluated.

The | (or) Operator

The | operator is placed between two or more conditional expressions and will check the result of each and every expression from left to right. If one or more of the conditional expressions evaluate to true, the total result will be true. If all of the conditional expressions evaluate to false, the total result will be false.
String favoriteColor = "blue";
int a = 5;
if (favoriteColor == "green" | favoriteColor == "blue" | favoriteColor == "red" | ++a == 11) {
     System.out.println("At least one expression evaluated to true");
}
System.out.println("a is now equal to "+a);

favoriteColor = "purple";
if (favoriteColor == "green" | favoriteColor == "blue" | favoriteColor == "red" | ++a == 11) {
     System.out.println("This will not print, all expressions evaluated to false.");
}
System.out.println("a is now equal to "+a);

What is the difference between the | operator and the || operator? Good question, there is only one difference.
The | operator will evaluate each and every conditional expression.
The || operator will evaluate the expressions from left to right and if an expression evaluates to true, the total result will be true - any remaining expressions will not be evaluated.



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 AndOr
C:\Java>cd AndOr
C:\Java\AndOr>Notepad AndOr.java

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


class AndOr {
    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");
        }

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

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

        String favoriteColor = "blue"; 
        if (favoriteColor == "green" | favoriteColor == "blue" | favoriteColor == "red" | ++a == 11) {
            System.out.println("At least one expression evaluated to true");
        }
        System.out.println("a is now equal to "+a);
        System.out.println();

        favoriteColor = "purple"; 
        if (favoriteColor == "green" | favoriteColor == "blue" | favoriteColor == "red" | ++a == 11) {
            System.out.println("This will not print, all expressions evaluated to false.");
        }
        System.out.println("a is now equal to "+a);
    }
}

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


C:\Java\AndOr>javac AndOr.java
C:\Java\AndOr>java AndOr
c is greater than a and b
e is greater than a, b, c, and d

At least one expression evaluated to true
a is now equal to 6

a is now equal to 7


Final thoughts

Be sure to check out my tutorial on the short-circuit Logical Operators Tutorial: && (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