Day 8: Java Logical Operators & Conditional Statements
Master logical operators (AND, OR, NOT) and conditional statements (if, if-else, else-if) in Java programming
Today's Learning Objectives
Logical Operators
- • AND (&&) operator with truth tables
- • OR (||) operator with practical examples
- • NOT (!) operator for condition reversal
- • Multiple variable declarations
Conditional Statements
- • Simple if statements
- • if-else conditions
- • else-if ladder structures
- • Real-world programming examples
Logical Operators in Java
1. AND Operator (&&)
In AND operator, if both conditions are true then we get result is true.
Truth Table
Condition1 | Condition2 | Result |
---|---|---|
true | true | true |
true | false | false |
false | true | false |
false | false | false |
System.out.println(a && b); // true
System.out.println(a && c); // false
System.out.println(c && b); // false
System.out.println(c && d); // false
System.out.println((a < b) && (c < d)); // true
System.out.println((a < b) && (c > d)); // false
System.out.println((a > b) && (c < d)); // false
System.out.println((a > b) && (c > d)); // false
2. OR Operator (||)
In OR operator, if any one condition is true then we get result is true.
Truth Table
Condition1 | Condition2 | Result |
---|---|---|
true | true | true |
true | false | true |
false | true | true |
false | false | false |
System.out.println(a || b); // true
System.out.println(a || c); // true
System.out.println(c || b); // true
System.out.println(c || d); // false
3. NOT Operator (!)
NOT operator means reverse of original values. If condition is true then result is false, and vice versa.
Truth Table
Condition | Result |
---|---|
true | false |
false | true |
System.out.println(a); // true
System.out.println(!a); // false
System.out.println(b); // false
System.out.println(!b); // true
System.out.println(x > y); // true
System.out.println(!(x > y)); // false
Multiple Variable Declaration
You can define multiple variables of the same datatype in a single line.
System.out.println((a <= b) || (c != d)); // true
Java Conditional Statements
There are 2 types of conditional statements in Java: if-else conditions and switch statements. Today we'll focus on if-else conditions.
1. Simple if Statement
If block condition is true then it executes if block of code, otherwise it skips it.
Syntax
{
// Java statements
}
public static void main(String[] args) {
System.out.println("Main method started");
int a = 100;
int b = 240;
if(a < b) {
System.out.println("Pune...");
}
System.out.println("Main method ended");
}
}
2. if-else Statement
If condition is true, execute if block. If condition is false, execute else block.
Syntax
// if block code
} else {
// else block code
}
int b = 200;
if(a >= b) {
System.out.println("Pune..");
} else {
System.out.println("Mumbai..");
}
3. else if
Multiple conditions checked sequentially. First true condition executes its block and skips remaining conditions.
Syntax
// condition 1 block
} else if(condition2) {
// condition 2 block
} else if(condition3) {
// condition 3 block
} else {
// else block
}
if((a > b) && (a > c)) {
System.out.println("a is bigger number");
} else if(b > c) {
System.out.println("b is bigger number");
} else {
System.out.println("c is bigger number");
}
Real-world Examples
Age Category Classification
if((age >= 1) && (age <= 11)) {
System.out.println("child");
} else if((age >= 12) && (age <= 17)) {
System.out.println("teen");
} else if((age >= 18) && (age <= 64)) {
System.out.println("adult");
} else if(age > 64) {
System.out.println("senior");
} else {
System.out.println("Please enter valid age");
}
Grade Classification System
if(marks < 25) {
System.out.println("Grade is F");
} else if((marks >= 25) && (marks <= 45)) {
System.out.println("Grade is E");
} else if((marks >= 46) && (marks <= 55)) {
System.out.println("Grade is D");
} else if((marks >= 56) && (marks <= 65)) {
System.out.println("Grade is C");
} else if((marks >= 66) && (marks <= 75)) {
System.out.println("Grade is B");
} else {
System.out.println("Grade is A");
}
Day 8 Knowledge Check
What is the result of (true && false) || (false && true)?
Key Takeaways
Logical Operators
- AND (&&): Both conditions must be true → result is true
- OR (||): Any one condition true → result is true
- NOT (!): Reverses boolean value (true ↔ false)
- Multiple Variables: int a = 10, b = 20, c = 30;
Truth Tables
- AND: T&&T=T, T&&F=F, F&&T=F, F&&F=F
- OR: T||T=T, T||F=T, F||T=T, F||F=F
- NOT: !T=F, !F=T
- Use parentheses for complex expressions
Conditional Statements
- if: Execute block only if condition is true
- if-else: Execute if block or else block
- else if: Multiple conditions, first true executes
- Use logical operators in conditions for complex logic
Real-world Applications
- Age Categories: Child (1-11), Teen (12-17), Adult (18-64), Senior (65+)
- Grade System: F(<25), E(25-45), D(46-55), C(56-65), B(66-75), A(75+)
- Number Classification: Small, Medium, Big, Biggest ranges
- Decision Making: User input validation, menu systems
💡 Master these logical operators and conditional statements as they form the foundation for decision-making in programming. Day 9 will introduce loops for repetitive operations.