Have a question?
Message sent Close
View Categories

What is logical operators in Java? 

What is logical operators in Java? 

📄
filename.js
public class LogicalOperatorExample {

    public static void main(String[] args) {

        int a = 10;

        int b = 20;

        /* Check if a is less than 15 
           AND b is greater than 15 */

        if (a < 15 && b > 15) {

            System.out.println("Both conditions are true");

        } else {

            System.out.println("At least one condition is false");

        }

    }

}

Output:
Both conditions are true

📄
filename.js
public class LogicalOperatorExample {

    public static void main(String[] args) {

        int a = 10;

        int b = 5;

        // Check if a is greater than 15 OR b is less than 10

        if (a > 15 || b < 10) {

            System.out.println("At least one condition is true");

        } else {

            System.out.println("Both conditions are false");

        }

    }

}

Output:
At least one condition is true

📄
filename.js
public class LogicalOperatorExample {

    public static void main(String[] args) {

        boolean condition = false;

        // Check if the condition is NOT true

        if (!condition) {

            System.out.println("Condition is false");

        } else {

            System.out.println("Condition is true");

        }

    }

}

Output:
Condition is false

📄
filename.js
public class LogicalOperatorExample {

    public static void main(String[] args) {

        int a = 10;

        int b = 20;

        int c = 5;

        /* Check if a is less than 15 AND 
        (b is greater than 15 OR c is greater than 10) */

        if (a < 15 && (b > 15 || c > 10)) {

            System.out.println("Complex condition is true");

        } else {

            System.out.println("Complex condition is false");

        }

    }

}

Output:
Complex condition is true