Table of contents | |
Introduction to Logical Operators | |
AND Operator (&&) | |
OR Operator (||) | |
NOT Operator (!) | |
Sample Problems | |
Conclusion |
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.
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:
Now let's dive into each of these operators with examples to understand their functionality better.
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.
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.
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.
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.
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.
60 videos|37 docs|12 tests
|
|
Explore Courses for EmSAT Achieve exam
|