Logical operators: &&, || and !
22 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)A logical operator joins true/false values. && (AND) needs both sides to be true, || (OR) needs at least one, and ! (NOT) flips a value.
&& (AND): both must be true
To sit an exam, a student needs 75% attendance and paid fees. One comparison cannot check both.
The AND operator && gives true only when both sides are true.
Example 1 · Can the student sit the exam?
public class Exam { public static void main(String[] args) { int attendance = 80; boolean hasPaidFees = false; boolean canSit = attendance >= 75 && hasPaidFees; System.out.println("Can sit exam: " + canSit); boolean isValid = attendance >= 0 && attendance <= 100; System.out.println("Valid attendance: " + isValid); }}Can sit exam: false Valid attendance: true
What this code does
- Attendance is fine, but the fees are not paid, so
&&givesfalse. - The range check is two comparisons joined with
&&: 80 is at least 0 and at most 100.
The maths style 18 <= age <= 60 does not compile. Write two comparisons and join them with &&: age >= 18 && age <= 60.
|| (OR): at least one must be true
A cinema gives a discount to students or senior citizens, and someone who is both gets it too.
The OR operator || gives true when at least one side is true, and false only when both are false.
Example 2 · Who gets the discount?
public class Discount { public static void main(String[] args) { boolean riyaIsStudent = true; int riyaAge = 20; System.out.println("Riya: " + (riyaIsStudent || riyaAge >= 60)); boolean raviIsStudent = false; int raviAge = 45; System.out.println("Ravi: " + (raviIsStudent || raviAge >= 60)); }}Riya: true Ravi: false
What this code does
- Riya is a student, so
||givestrue; onetrueside is enough. - Ravi is not a student and is under 60. Both sides are
false, so||givesfalse.
| key is usually above Enter; press it with Shift.! (NOT): flip the value
Your app stores hasPaidFees, but a reminder screen needs the opposite: are the fees still pending?
The NOT operator ! flips a value: !true is false, so !hasPaidFees is true exactly when the fees are pending.
&&, || and ! work only on boolean values. !marks with an int gives the error bad operand type int for unary operator '!'. Flip the switches below to see all three at work.
Experiment
Truth switches
Turn a and b on and off. Find the only case where a || b is false.
- a && b
- false
- a || b
- true
- !a
- false
- !b
- true
Short-circuit: Java stops early
An app checks totalMarks / students > 50, but when students is 0 the division stops the program, as in Arithmetic operators (opens in a new tab).
Short-circuit: Java skips the right side when the left side already decides the answer.
Example 3 · Short-circuit keeps you safe
public class SafeDivide { public static void main(String[] args) { int students = 0; int totalMarks = 0; boolean goodAverage = students != 0 && totalMarks / students > 50; System.out.println("Good average: " + goodAverage); }}Good average: false
What this code does
students != 0isfalse, so&&already knows the answer isfalseand stops there.- The division never runs, so there is no
/ by zeroerror.
With ||, a true left side decides the answer, so the right side is skipped too. Put the safety check on the left: swap the two sides and the division runs first, and the program stops.
& and |, which always run both sides. For conditions, use && and ||.Next step
Check your understanding
10 quick questions