Logical Operators in Java
×


Logical Operators in Java

226

Logical Operators in Java

In Java, logical operators are used to make decisions based on multiple conditions. These operators return a boolean value (true or false) depending on the logic applied to the operands.

Logical operators are often used in if, while, and for statements to control the flow of a program.

Types of Logical Operators

Java supports three main logical operators:

  • && (Logical AND)
  • || (Logical OR)
  • ! (Logical NOT)

Logical AND (&&)

The && operator returns true only if both conditions are true. If either one is false, the result will be false.

int a = 10;
int b = 20;
System.out.println(a > 5 && b > 15);  // true
System.out.println(a > 15 && b > 10); // false
  

Logical OR (||)

The || operator returns true if at least one of the conditions is true. It only returns false when both conditions are false.

int a = 5;
int b = 8;
System.out.println(a > 3 || b < 5);   // true
System.out.println(a < 3 || b < 5);   // false
  

Logical NOT (!)

The ! operator reverses the boolean value of an expression. If a condition is true, applying ! will make it false, and vice versa.

boolean isJavaFun = true;
System.out.println(!isJavaFun);  // false

int x = 10;
System.out.println(!(x > 5));    // false
System.out.println(!(x < 5));    // true
  

Combining Logical Operators

Logical operators can be combined to form complex conditions. This is helpful when evaluating multiple criteria in a single statement.

int age = 25;
int salary = 50000;

if(age > 18 && salary > 30000) {
    System.out.println("Eligible for loan.");
} else {
    System.out.println("Not eligible.");
}
// Output: Eligible for loan.
  

Short-Circuit Behavior

Java uses short-circuit evaluation with && and ||. This means if the result can be determined by the first condition, the second one won’t be evaluated.

int a = 5;
int b = 10;

if(a > 0 || b++ > 5) {
    System.out.println("True condition");
}
System.out.println("b = " + b);  // Output: b = 10 (b++ is not executed)
  

Conclusion

Logical Operators in Java are essential for building conditions and decision-making logic. Whether you're validating user input, checking multiple states, or controlling program flow, these operators are your go-to tools.

Practice using them in different scenarios to become more confident in writing logical code.



If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!

For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments

Coding Tag WhatsApp Chat