"Greetings, Amigo! I was so carried away by solving logic problems that I didn't notice you come in. Here's one for you: if crocodiles fly, then snow is white. There's a flying crocodile. What's the conclusion?"

"Um... So we conclude that snow is white?"

"Excellent! You passed the initiation test. You are ready to master the next topic. It is called 'logical operators'. And we'll start with Boolean logic. Instinctively, you already know it. You're a robot, after all. We just need to tweak your settings to match the Java language."

"Boolean logic? I was recently told about the boolean type..."

"Yes, there's a direct connection here. Boolean expressions can only be true or false. And as it happens, this sort of logic is all about expressions that are true or false, and combinations of such expressions. For example, the expression 18 < 25 is always true, and 7 < 5 is always false. The expression (i < 10) depends on the value of i. And if the expression evaluates, for example, to true, then we can ask the program to do something."

"Ahh, I get it. Boolean expressions allow us not only to make logical conclusions, but also to create forks in programs."

"Exactly. The main thing is to learn how to write them. For example, in Java you can't just go and write the expression 18 < age <65. This would be syntactically incorrect and the program will not compile.

"But you can write it like this:

            (18 < age) AND (age < 65)
        

Of course, we don't actually use the ENGLISH word AND. Instead, you need a boolean operator. That is, 'AND' is represented differently.

"There are three logical operators in Java: AND (&&), OR (||), NOT (!).

The good news is that you can use parentheses to construct logical expressions of any complexity.

The bad news is that Java developers decided to use notation from the C language instead of the words and, or and not.

Look at the screen:

Logical operator Expectation Reality
AND (∧) and &&
OR (∨) or ||
NOT (¬) not !

"It's actually not that bad... Pretty compact. I've almost memorized them."

"Well, that's marvelous. Here are some examples of using logical operators in Java:

Expression Interpretation Explanation
(0 < a) && (a < 100) (0 < a) and (a < 100) (0 < a) AND (a < 100)
(!a) && (!b) (not a) and (not b) (NOT a) AND (NOT b)
!(!a || !b) not((not a) or (not b)) NOT((NOT a) OR (NOT b))

Examples of using comparison operators and boolean variables

"Remember, Amigo, wherever you can write a logical expression, you can write a logical variable."

"How's that?"

"I mean you can write logical expressions in different ways. For instance:

Code Explanation
int age = 35;
if (age >= 18 && age <= 65)
   System.out.println("You can work");
If the value of age is between 18 and 65, then the phrase "You can work" is displayed.
int age = 35;
boolean isYoung = (age < 18);
if (!isYoung && age <= 65)
   System.out.println("You can work");
We created an isYoung variable and moved the first part of the expression into it. We simply replaced age >= 18 with age < 18.
int age = 35;
boolean isYoung = (age < 18);
boolean isOld = (age > 65);
if (!isYoung && !isOld)
   System.out.println("You can work");
We created an isOld variable and moved the second part of the expression into it. Additionally, age <= 65 was replaced by age > 65.

"These three examples are equivalent. Only in the second example did we move part of the expression from the if statement into a separate boolean variable (isYoung). In the third example, we moved the second part of the expression into a second variable (isOld). By the way, the default value of a boolean variable is false."

"I will remember that. I hope."

Logical arithmetic

"Now let's briefly go through logical operations. They obey very simple and logical (how could it be otherwise!) rules.

"First, let's see how the OR operator works. It's also known as || or disjunction.

Expression Result
true || true true
true || false true
false || true true
false || false false

"Can you deduce now what the result of the expression a || b is based on the table?"

"I see!" The value of an expression is true if at least one term in the expression is true. If both are false, then the result is false.

"That's correct. Since you're so smart, take another look at the table and imagine that false is 0 and true is 1. When you look at it that way, does the || operator's behavior remind you of anything from ordinary arithmetic?"

"Um... Well, it's a bit like addition... But when you do addition, 1 + 1 does not equal 1."

"There is a sense in which it is equal if we are only working with 0 and 1. But don't bother with that right now. The important thing is that you noticed the similarity between the || operation and addition. This means you won't be surprised by the fact that this operation is often called 'logical addition'.

"Got it."

"Now the AND, aka &&, aka conjunction operator, comes onto the stage.

Expression Result
true && true true
true && false false
false && true false
false && false false

"As I understand it, the result of an expression is true only if both values that make up the expression are true. Otherwise, it is always false."

"Well done, Amigo! You're absorbing all this like a sponge. Do you see another similarity with arithmetic?"

"Multiplication!"

"Exactly. So here we have a 'logical multiplication'".

"Next we look at the NOT operator, aka !, aka the logical inverse.

Expression Result
!true false
!false true

"Well, everything is quite simple here. The operator changes true to false and vice versa."

"Exactly. Here are some useful expressions for you:"

Expression Result
m && !m false
m || !m true
!(a && b) !a || !b
!(a || b) !a && !b