EmSAT Achieve Exam  >  EmSAT Achieve Notes  >  Java for EmSAT Achieve  >  Java Logical Operators

Java Logical Operators | Java for EmSAT Achieve PDF Download

Logical operators are an essential component of any programming language, including Java. They allow us to combine multiple conditions and make decisions based on their outcomes. In this article, we will explore the logical operators in Java, understand how they work, and provide numerous examples to solidify your understanding.

Introduction to Logical Operators

Logical operators in Java are used to evaluate multiple conditions and produce a single boolean result: either true or false. There are three logical operators in Java:

  • AND operator (&&): Returns true if both conditions on the left and right of the operator are true. Otherwise, it returns false.
  • OR operator (||): Returns true if either condition on the left or right of the operator is true. It returns false only if both conditions are false.
  • NOT operator (!): Reverses the boolean value of a condition. If a condition is true, the NOT operator makes it false, and vice versa.

Now let's dive into each of these operators with examples to understand their functionality better.

AND Operator (&&)

The AND operator returns true if both conditions are true. Otherwise, it returns false. Let's consider a simple example:

int age = 25;

boolean isStudent = true;


if (age >= 18 && isStudent) {

    System.out.println("You are eligible for a student discount.");

} else {

    System.out.println("Sorry, you are not eligible for a student discount.");

}

Output

You are eligible for a student discount.

In this example, we check if the age is greater than or equal to 18 and the isStudent variable is true. Since both conditions are true, the message "You are eligible for a student discount" is printed.

Let's see another example:

int num = 10;


if (num > 5 && num < 15) {

    System.out.println("The number is between 5 and 15.");

} else {

    System.out.println("The number is outside the range.");

}

Output

The number is between 5 and 15.

In this case, the condition num > 5 && num < 15 is true because num is both greater than 5 and less than 15.

OR Operator (||)

The OR operator returns true if at least one of the conditions is true. It returns false only if both conditions are false. Let's see an example:

boolean isSunny = true;

boolean isRainy = false;


if (isSunny || isRainy) {

    System.out.println("The weather is either sunny or rainy.");

} else {

    System.out.println("The weather is neither sunny nor rainy.");

}

Output

The weather is either sunny or rainy.

Here, the condition isSunny || isRainy is true because isSunny is true. Therefore, the message "The weather is either sunny or rainy" is printed.

Now, let's consider another example:

int x = 5;


if (x < 0 || x > 10) {

    System.out.println("The number is either negative or greater than 10.");

} else {

    System.out.println("The number is between 0 and 10 (inclusive).");

}

Output

The number is between 0 and 10 (inclusive).

In this case, the condition x < 0 || x > 10 is false because x is neither negative nor greater than 10.

NOT Operator (!)

The NOT operator (!) reverses the boolean value of a condition. Let's see an example:

boolean isRainy = false;


if (!isRainy) {

    System.out.println("It's not raining.");

} else {

    System.out.println("It's raining.");

}

Output

It's not raining.

In this example, the condition !isRainy is true because isRainy is false. Therefore, the message "It's not raining" is printed.

Let's consider another example:

int age = 15;


if (!(age >= 18)) {

    System.out.println("You are underage.");

} else {

    System.out.println("You are eligible.");

}

Output

You are underage.

In this case, the condition !(age >= 18) is true because age >= 18 is false. Thus, the message "You are underage" is printed.

Sample Problems

1. Write a Java program to check if a number is both even and divisible by 5. If it satisfies both conditions, print "Valid number," otherwise print "Invalid number."

int number = 20;


if (number % 2 == 0 && number % 5 == 0) {

    System.out.println("Valid number.");

} else {

    System.out.println("Invalid number.");

}

Output

Valid number.

2. Write a Java program to check if a character is a vowel (a, e, i, o, u) or a digit. If it is either a vowel or a digit, print "Valid character," otherwise print "Invalid character."

char ch = '5';


if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9')) {

    System.out.println("Valid character.");

} else {

    System.out.println("Invalid character.");

}

Output

Valid character.

Conclusion

Logical operators (&&, ||, and !) are crucial tools in Java for evaluating conditions and making decisions based on their outcomes. By combining multiple conditions, you can create powerful and flexible programs. Understanding how these operators work and practicing with different examples will help you enhance your programming skills. Keep exploring and experimenting with logical operators to gain more confidence in using them effectively in your Java programs.

The document Java Logical Operators | Java for EmSAT Achieve is a part of the EmSAT Achieve Course Java for EmSAT Achieve.
All you need of EmSAT Achieve at this link: EmSAT Achieve
60 videos|37 docs|12 tests

Top Courses for EmSAT Achieve

60 videos|37 docs|12 tests
Download as PDF
Explore Courses for EmSAT Achieve exam

Top Courses for EmSAT Achieve

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

video lectures

,

Objective type Questions

,

Sample Paper

,

shortcuts and tricks

,

Extra Questions

,

Previous Year Questions with Solutions

,

mock tests for examination

,

Viva Questions

,

MCQs

,

Exam

,

Java Logical Operators | Java for EmSAT Achieve

,

Semester Notes

,

practice quizzes

,

past year papers

,

Java Logical Operators | Java for EmSAT Achieve

,

Java Logical Operators | Java for EmSAT Achieve

,

pdf

,

study material

,

Important questions

,

Free

,

Summary

,

ppt

;