Tutorials Logic, IN info@tutorialslogic.com
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Website Development
Practice
Quiz Challenge Interview Questions Certification Practice
Tools
Online Compiler JSON Formatter Regex Tester CSS Unit Converter Color Picker
Compiler Tools

Control Flow in Java if, else, switch: Tutorial, Examples, FAQs & Interview Tips

if / if-else / if-else-if

The if statement executes a block of code only when a condition is true. Chain multiple conditions with else if.

if / else-if / else
public class IfElse {
    public static void main(String[] args) {
        int score = 75;

        if (score >= 90) {
            System.out.println("Grade: A");
        } else if (score >= 80) {
            System.out.println("Grade: B");
        } else if (score >= 70) {
            System.out.println("Grade: C");
        } else if (score >= 60) {
            System.out.println("Grade: D");
        } else {
            System.out.println("Grade: F");
        }
        // Output: Grade: C
    }
}

switch Statement

Use switch when you need to compare a single variable against multiple constant values. Java 14+ introduced the switch expression with arrow syntax - cleaner and no fall-through.

Traditional & Enhanced Switch
public class SwitchDemo {
    public static void main(String[] args) {
        int day = 3;

        // Traditional switch (requires break to prevent fall-through)
        switch (day) {
            case 1: System.out.println("Monday");    break;
            case 2: System.out.println("Tuesday");   break;
            case 3: System.out.println("Wednesday"); break;
            case 4: System.out.println("Thursday");  break;
            case 5: System.out.println("Friday");    break;
            default: System.out.println("Weekend");
        }

        // Enhanced switch expression (Java 14+) - no break needed
        String dayName = switch (day) {
            case 1 -> "Monday";
            case 2 -> "Tuesday";
            case 3 -> "Wednesday";
            case 4 -> "Thursday";
            case 5 -> "Friday";
            default -> "Weekend";
        };
        System.out.println(dayName);  // Wednesday

        // Switch with String
        String season = "SUMMER";
        String activity = switch (season) {
            case "SPRING" -> "Plant flowers";
            case "SUMMER" -> "Go swimming";
            case "AUTUMN" -> "Rake leaves";
            case "WINTER" -> "Build a snowman";
            default -> "Stay home";
        };
        System.out.println(activity);  // Go swimming
    }
}

Ternary Operator

The ternary operator is a compact one-liner for simple if-else assignments: condition ? valueIfTrue : valueIfFalse.

Ternary Operator
public class Ternary {
    public static void main(String[] args) {
        int a = 10, b = 20;

        // Simple ternary
        int max = (a > b) ? a : b;
        System.out.println("Max: " + max);  // 20

        // Nested ternary (use sparingly - hurts readability)
        int num = 0;
        String sign = (num > 0) ? "positive" : (num < 0) ? "negative" : "zero";
        System.out.println(num + " is " + sign);  // 0 is zero

        // Ternary in print
        boolean isLoggedIn = true;
        System.out.println("User is " + (isLoggedIn ? "online" : "offline"));
    }
}
Key Takeaways
  • Java supports if-else, switch, for, while, do-while, break, continue, and return.
  • Java 14+ enhanced switch expressions can return values and use arrow syntax.
  • The enhanced for-each loop (for (Type item : collection)) is cleaner for iterating arrays and collections.
  • break with a label exits a specific outer loop - useful for nested loop scenarios.
  • Java switch uses strict equality - it supports int, String, char, and enum types.
  • Avoid deeply nested if-else chains - use early returns or switch expressions for cleaner code.

Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.