CodeGym /Courses /Java Syntax Zero /Logical operators

Logical operators

Java Syntax Zero
Level 3 , Lesson 6
Available

1. Boolean logic

In Java, you can't write the expression 18 < age <65. That is incorrect syntax and the program will not compile.

But you can write it like this:

            (18 < age) AND (age < 65)
        

Of course, instead of the word AND, there would be a logical operator. We'll talk about them in more detail now.

There are three logical operators in Java: AND (&&), OR (||) and 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 !

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))


2. Examples of using comparison operators and boolean variables

Wherever you can write a logical expression, you can write a logical variable.

Example:

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, we replaced age <= 65 with 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).



3. Logical arithmetic

Let's briefly go through logical operations.

The AND operator is &&, also known as conjunction.

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

In other words, the result of an expression is true only if both values that make up the expression are true. Otherwise, it is always false.

The OR operator is ||, also known as disjunction.

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

In other words, the result of an expression is always true if at least one term in the expression is true. If both are false, then the result is false.

The NOT operator is !, also known as the logical inverse.

Expression Result
!true
false
!false
true

The operator changes true to false and vice versa.

Useful expressions:

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

Comments (21)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Serg Odin Level 4, Los Altos, United States
29 June 2024
Interesting heh!
Edwin Lara Level 3
25 February 2024
In the Triangle exercise: With the "Correct" code, If I enter two equal sides and one longer/shorter, I get the wrong answer, WHY?
haalk3n Level 6, Romania
29 January 2024
Hello 2024!
Strex Level 6, Canada
12 December 2023
Pumped, this made sense and I'm feeling progress!
larsintech Level 15, Switzerland Expert
29 July 2023
Isn't !(!a || !b) the same as a && b?
Jesú Level 14, Madrid, Spain
16 October 2023
Yes, look up De Morgen's law
Morakinyo Level 4, Lagos, Nigeria
25 November 2023
That's right
wisp Level 6, United States
6 March 2023
This lesson helped me a TON thank you Code gym. :)
alexhanson1002 Level 5, United States of America, United States
19 February 2023
Rather than only letting it decide it's the correct solution if it's written exactly the way it wants you to, it should be based on the execution and if the code executes write. I'm spending time trying to figure out what's wrong with my code but there's nothing, it's just a nuance in the way my code was written vs. the way the websites code was written.
Abdulaziz alshaikh Level 1, Saudi Arabia
6 February 2023
The last task a very mathmateic and i don't get it , maybe it's language barrier:( skipping
12 October 2023
If the task appears big, follow the step by step breakdown of the task into smaller tasks...
Abdulaziz alshaikh Level 1, Saudi Arabia
4 February 2023
it took me about 2hrs on the first task only, i need a break😪
Anonymous #11229118 Level 6, Montréal, Canada
25 December 2022
In the second task (To work or not to work? That is the question) one of the requirements is that the ages of 20 to 60 must be inclusive ( [ 20, 60 ] or age <= 20 & age >= 60 ). However it doesn't work, it seems they made a mistake 20 to 60 must be exclusive ( ] 20, 60 [ or age < 20 & age > 60 ).
JoePROgrammer Level 3, Eastern Samar, Philippines
19 January 2023
If the age is in the range from 20 to 60 (inclusive) then dont display anything. if (age < 20) || (age > 60); System.out.print("You dont have to work"); if user enters 20 to 60, both conditions become false, skips the print statement and wont display anything - meets the requirement.