Skip to content
C

Logical Operators

Logical operators combine two or more boolean conditions into a single result. Java provides AND (&&), OR (||), and NOT (!) to build these combined conditions.


1. What are Logical Operators?

Logical operators combine two or more boolean conditions into a single result. Java provides AND (&&), OR (||), and NOT (!) to build these combined conditions.

2. Why is it used?

Real decisions often depend on more than one condition at once — like allowing login only if both the username AND password are correct. Logical operators let a program handle these multiple conditions together.

3. Real-Life Example

Think of an exam eligibility rule: "You can appear for the exam only if your attendance is above 75% AND your fees are fully paid." Both conditions must be true together — exactly how the && operator behaves.

4. Syntax

java
condition1 && condition2 // true only if both are true condition1 || condition2 // true if at least one is true !condition // reverses true to false, and false to true

5. Example Program

java
public class LogicalDemo { public static void main(String[] args) { int attendance = 80; boolean feesPaid = true; boolean isEligible = (attendance > 75) && feesPaid; System.out.println("Exam Eligible: " + isEligible); } }

Output:

Exam Eligible: true

6. Key Points to Remember

  • && and || use "short-circuit" evaluation — if the first condition already decides the result, the second condition is not even checked.
  • ! simply flips a boolean's value.
  • Don't confuse logical operators (&&, ||) with bitwise operators (&, |) — they look similar but behave differently.

Mock Test

  • Logical Operators - Quick Test

    10 questions on &&, || and ! and how they combine boolean conditions.

    10 questions · 10 min · Easy
    Start Mock Test

Coding Problems