Slide 1
Slide 2
Slide 3

Logical Operators in java programming (V20)

No Comments

Logical Operators

Logical operators in Java are used to perform logical operations on boolean values. These operators are mainly used in conditional statements and loops to control the flow of execution.

Assume Boolean variable A stores the value true and variable B stores the value false, then:

Operator Description Example
&& (logical and) Returns true only if both conditions are true (A && B) is false
|| (logical or) Returns true if at least one condition is true (A || B) is true
! (logical not) Reverses the Boolean value !(A && B) is true

Source Code:

Copied!
public class LogicalOperators {
    public static void main(String[] args) {
        int a = 10, b = 20, c = 30;	
        // Logical AND (&&)
        if (a < b && b < c) {
            System.out.println("Both conditions are true (a < b AND b < c)");
        }
	        // Logical OR (||)
        if (a > b || b < c) {
            System.out.println("At least one condition is true (a > b OR b < c)");
        }
	        // Logical NOT (!)
        boolean isJavaFun = true;
        if (!isJavaFun) {
            System.out.println("Java is NOT fun");
        } else {
            System.out.println("Java is fun!");
        }
	        // Combining Logical Operators
        if ((a < b || b > c) && !(a == 10)) {
            System.out.println("Complex condition met!");
        } else {
            System.out.println("Complex condition NOT met!");
        }
    }
}

Result:

Watch the video:


Ebook: https://softkhpc.blogspot.com/2025/05/java-programming-ebooks.html

back to top