Java Programming

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

Condition1Condition2Result
truetruetrue
truefalsefalse
falsetruefalse
falsefalsefalse
// Boolean AND Example
boolean
a = true;
boolean
b = true;
boolean
c = false;
boolean
d = 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
// Integer Comparison AND Example
int
a = 100, b = 150, c = 23, d = 430;

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

Condition1Condition2Result
truetruetrue
truefalsetrue
falsetruetrue
falsefalsefalse
// Boolean OR Example
boolean
a = true;
boolean
b = true;
boolean
c = false;
boolean
d = 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

ConditionResult
truefalse
falsetrue
// NOT Operator Example
boolean
a = true;
boolean
b = false;

System.out.println(a); // true
System.out.println(!a); // false
System.out.println(b); // false
System.out.println(!b); // true

// With comparisons
int
x = 100, y = 23;
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.

// Syntax
datatype variableName1 = value1, variableName2 = value2, variableName3 = value3;

// Example
int
a = 100, b = 334, c = 345, d = 33;

// Using in logical operations
System.out.println((a <= b) && (c != d)); // true
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

if(condition)
{
    // Java statements
}
// Example
public class Demo7 {
    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(condition) {
    // if block code
} else {
    // else block code
}
// Example
int a = 100;
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

if(condition1) {
    // condition 1 block
} else if(condition2) {
    // condition 2 block
} else if(condition3) {
    // condition 3 block
} else {
    // else block
}
// Find maximum number example
int a = 10000, b = 800, c = 230;

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

int age = 25;

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

int marks = 85;

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

Question 1 of 5

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.

SDET Mastery

Master Test Automation

Home
CurriculumPracticeQ&ACheatsheet
🍵Buy me a Chai

Automation Testing Course

Comprehensive course covering Manual Testing, Java Programming, and Selenium WebDriver

🍵Buy me a Chai
Privacy Policy•GitHub
© 2024 Automation Testing Course. All rights reserved.