JAVA - Zero to Hero / Java Fundamentals

Conditional Statements in JAVA

❔What Are Conditional Statements?

Conditional statements allow you to make decisions in code.

They help execute certain parts of code only if specific conditions are true.

⚖️ if Statement

Executes a block only when the condition is true.

🔸 Syntax:

if (condition) {
    // code to run if true
}

🔸 Code Example:

public class MarksExample {
    public static void main(String[] args) {
        int marks = 85;

        if (marks > 80) {
            System.out.println("Excellent!");
        }

        if (marks < 50) { // Condition is false (85 < 50 is false), so this block won't execute
            System.out.println("Needs Improvement.");
        }
    }
}
Excellent!

🔍 Why use it?

To check and respond to a single condition.


🤔 if-else Statement

Executes one block if true, another block if false.

🔸 Code Example:

int age = 16;

if (age >= 18) {
    System.out.println("You can vote.");
} else {
    System.out.println("Too young to vote.");
}
Too young to vote.

✅ Condition:Age Greater Than 18

public class VotingCheck {
    public static void main(String[] args) {
        int age = 21;

        if (age >= 18) {
            System.out.println("You can vote."); // This block will run
        } else {
            System.out.println("Too young to vote.");
        }
    }
}
You can vote.

🔍 Why use it?

To handle two possibilities (true or false).


🔍 if-else if-else Ladder

Used when there are multiple conditions to test.

🔸 Code Example:

public class GradeChecker {
    public static void main(String[] args) {
        // Case 1: Grade A
        int score = 92;
        System.out.print("Score: " + score + " → ");
        if (score >= 90) {
            System.out.println("Grade A");
        } else if (score >= 75) {
            System.out.println("Grade B");
        } else if (score >= 60) {
            System.out.println("Grade C");
        } else {
            System.out.println("Fail");
        }

        // Case 2: Grade B
        score = 76;
        System.out.print("Score: " + score + " → ");
        if (score >= 90) {
            System.out.println("Grade A");
        } else if (score >= 75) {
            System.out.println("Grade B");
        } else if (score >= 60) {
            System.out.println("Grade C");
        } else {
            System.out.println("Fail");
        }

        // Case 3: Fail
        score = 45;
        System.out.print("Score: " + score + " → ");
        if (score >= 90) {
            System.out.println("Grade A");
        } else if (score >= 75) {
            System.out.println("Grade B");
        } else if (score >= 60) {
            System.out.println("Grade C");
        } else {
            System.out.println("Fail");
        }
    }
}
Score: 92 → Grade A
Score: 76 → Grade B
Score: 45 → Fail

🔍 Why use it?

When you need to check multiple ranges or options.


🔀 switch Statement

Used to replace long if-else chains when comparing a single variable to multiple constant values.

🔸 Syntax:

switch (variable) {
    case value1:
        // code
        break;
    case value2:
        // code
        break;
    default:
        // code
}

🔸 Code Example:

public class ValidDay {
    public static void main(String[] args) {
        int day = 3;

        System.out.print("Day " + day + ": ");
        switch (day) {
            case 1: System.out.println("Monday"); break;
            case 2: System.out.println("Tuesday"); break;
            case 3: System.out.println("Wednesday"); break;
            default: System.out.println("Invalid day");
        }
    }
}
Day 3: Wednesday

🔍 Why use it?

For cleaner multi-value comparisons based on one variable.


public class InvalidDay {
    public static void main(String[] args) {
        int day = 8;

        System.out.print("Day " + day + ": ");
        switch (day) {
            case 1: System.out.println("Monday"); break;
            case 2: System.out.println("Tuesday"); break;
            case 3: System.out.println("Wednesday"); break;
            default: System.out.println("Invalid day");
        }
    }
}
Day 8: Invalid day