Logical Operators
- Logical operators are used to form logical expressions.
- The operands of these operators will be boolean type.
- Result of logical expression will be boolean type i.e. true or false.
Logical Operators Table
| Logical Operator | Description | | ---------------- | ----------- | --- | ---------- | | ! | Logical NOT | | && | Logical AND | | | | | Logical OR |
6.1 Logical NOT Operator
- It is a unary operator.
- It is used to inverse the result.
Truth Table
| Operand | Result |
|---|---|
| true | false |
| false | true |
Lab138.java
class Lab138 {
public static void main(String[] args) {
System.out.println(!true);
System.out.println(!false);
}
}
Lab139.java
class Lab139 {
public static void main(String[] args) {
int a = 10;
System.out.println(!a);
}
}
Lab140.java
class Lab140 {
public static void main(String[] args) {
int a = 10;
System.out.println(!a == 10);
}
}
Lab141.java
class Lab141 {
public static void main(String[] args) {
int a = 10;
System.out.println(!(a == 10));
}
}
Logical OR and Logical AND
- These are binary operators.
- Logical OR and Logical AND operators are used to combine two relational expressions to form logical expressions.
Example:
(a>b)&& (a>c)
(a<b)|| (a<c)
6.2 Logical AND Operator (&&)
- Result of Logical AND is true if both the operands are true otherwise false.
Truth Table
| OP1 | OP2 | Result |
|---|---|---|
| true | true | true |
| true | false | false |
| false | true | false |
| false | false | false |
Lab142.java
class Lab142 {
public static void main(String as[]) {
System.out.println(true && true);
System.out.println(true && false);
System.out.println(false && true);
System.out.println(false && false);
}
}
Lab143.java
class Lab143 {
public static void main(String as[]) {
int a = 99;
long b = 65799999;
boolean b1 = (a == 99) && (b == 65799999);
boolean b2 = (a == 99) && (b == 65799990);
System.out.println(b1);
System.out.println(b2);
}
}
Lab144.java
class Lab144 {
public static void main(String as[]) {
int a = 12;
boolean b = a > 10 && a++ < 5;
System.out.println(a);
System.out.println(b);
}
}
Lab145.java
class Lab145 {
public static void main(String as[]) {
int a = 12;
boolean b = a > 15 && a++ < 5;
System.out.println(a);
System.out.println(b);
}
}
6.3 Logical OR Operator (||)
- Result of Logical OR is false if both operands are false, otherwise true.
Truth Table
| OP1 | OP2 | Result |
|---|---|---|
| true | true | true |
| true | false | true |
| false | true | true |
| false | false | false |
Lab146.java
class Lab146 {
public static void main(String[] args) {
System.out.println(true || true);
System.out.println(true || false);
System.out.println(false || true);
System.out.println(false || false);
}
}
Lab147.java
class Lab147 {
public static void main(String[] args) {
int a = 99;
long b = 65799999;
boolean b1 = (a == 99) || (b == 65799999);
boolean b2 = (a == 99) || (b == 65799990);
boolean b3 = (a == 88) || (b == 65799990);
System.out.println(b1);
System.out.println(b2);
System.out.println(b3);
}
}
Lab148.java
class Lab148 {
public static void main(String[] args) {
int a = 12;
boolean b = a > 10 || a++ < 5;
System.out.println(a);
System.out.println(b);
}
}
Lab149.java
class Lab149 {
public static void main(String[] args) {
int a = 12;
boolean b = a > 15 || a++ < 5;
System.out.println(a);
System.out.println(b);
}
}