Logical operators help you combine or change true/false values to make decisions in your program.
Logical operators in Java
condition1 && condition2 // AND operator condition1 || condition2 // OR operator !condition // NOT operator
&& means both conditions must be true.
|| means at least one condition must be true.
! reverses true to false, and false to true.
if (age > 18 && hasID) { System.out.println("Allowed to enter"); }
if (isWeekend || isHoliday) { System.out.println("No work today"); }
if (!isRaining) { System.out.println("Go outside"); }
This program uses logical operators to decide if someone can drive, if they can relax today, and if they should go for a walk.
public class LogicalOperatorsDemo { public static void main(String[] args) { boolean hasLicense = true; int age = 20; if (age >= 18 && hasLicense) { System.out.println("You can drive."); } else { System.out.println("You cannot drive."); } boolean isWeekend = false; boolean isHoliday = true; if (isWeekend || isHoliday) { System.out.println("You can relax today."); } else { System.out.println("You have to work today."); } boolean isRaining = false; if (!isRaining) { System.out.println("Let's go for a walk."); } else { System.out.println("Better stay inside."); } } }
Remember that && stops checking if the first condition is false (short-circuit).
Similarly, || stops checking if the first condition is true.
Use parentheses to group conditions clearly when combining multiple logical operators.
Logical operators combine true/false values to control decisions.
&& means both must be true, || means one or both are true, ! reverses the value.
They help your program make smart choices based on multiple conditions.
Practice
Solution
Step 1: Understand the meaning of logical AND
The operator && means both conditions must be true for the whole expression to be true.Step 2: Compare with other operators
|| means one or both true, ! reverses true/false, ^ means exclusive or (one true only).Final Answer:
&& (AND) -> Option AQuick Check:
Both true = && [OK]
- Confusing || (OR) with && (AND)
- Thinking ! means AND
- Mixing up ^ with &&
flag in Java?Solution
Step 1: Recall the NOT operator syntax
In Java, the NOT operator is ! and placed before the boolean variable to reverse its value.Step 2: Check each option
flag = !flag; uses ! correctly. flag = not flag; uses 'not' which is invalid in Java. flag = ~flag; uses ~ which is bitwise NOT, not for booleans. flag = flag !=; is invalid syntax.Final Answer:
flag = !flag; -> Option BQuick Check:
Negate boolean with ! [OK]
- Using 'not' instead of !
- Using bitwise ~ on booleans
- Incorrect syntax like 'flag !=;'
boolean a = true;
boolean b = false;
System.out.println(a && b || !b);
Solution
Step 1: Evaluate the expression step-by-step
a && b is true && false = false. !b is !false = true.Step 2: Apply OR operator
Now false || true = true.Final Answer:
true -> Option CQuick Check:
(true && false) || !false = true [OK]
- Ignoring operator precedence
- Evaluating || before &&
- Confusing !b value
boolean x = true;
boolean y = false;
if (x & | y) {
System.out.println("Yes");
}
Solution
Step 1: Check the if condition syntax
The condition uses '& |' which is not a valid operator combination in Java.Step 2: Understand valid logical operators
Valid operators are &&, ||, &, | but not combined as '& |'. This causes a syntax error.Final Answer:
Invalid use of & | operators together -> Option AQuick Check:
Operators & and | cannot be combined like '& |' [OK]
- Confusing bitwise & with logical &&
- Combining & and | without operand
- Thinking no error occurs
n is between 10 and 20 inclusive in Java. Which condition correctly uses logical operators?Solution
Step 1: Understand the range condition
To check if n is between 10 and 20 inclusive, both conditions n >= 10 and n <= 20 must be true.Step 2: Analyze each option
if (n >= 10 || n <= 20) uses || which means either condition true, so it allows numbers outside the range. if (n >= 10 && n <= 20) uses && which correctly requires both conditions true. if (!(n < 10 && n > 20)) negates an impossible condition and is incorrect. if (n > 10 && n < 20) excludes 10 and 20 by using > and <.Final Answer:
if (n >= 10 && n <= 20) -> Option DQuick Check:
Both limits true = && [OK]
- Using || instead of && for range
- Excluding boundary values by using > or <
- Misusing negation for range check
Ready to prove it?
Pass the test and this pattern moves unknown
