Control Flow in Java

Control Flow in Java

Control flow statements determine the order in which your program executes code. Java provides various ways to control program flow.

if-else Statements

Basic if-else

IfElseExample.java
public class IfElseExample {
    public static void main(String[] args) {
        int age = 18;
        
        if (age >= 18) {
            System.out.println("You are an adult");
        } else {
            System.out.println("You are a minor");
        }
        
        // Multiple conditions
        int score = 85;
        
        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 {
            System.out.println("Grade: F");
        }
    }
}

Ternary Operator

TernaryExample.java
public class TernaryExample {
    public static void main(String[] args) {
        int number = 10;
        String result = (number % 2 == 0) ? "Even" : "Odd";
        
        System.out.println("The number is: " + result);
    }
}

switch Statements

Switch with ints

SwitchExample.java
public class SwitchExample {
    public static void main(String[] args) {
        int dayOfWeek = 3;
        String dayName;
        
        switch (dayOfWeek) {
            case 1:
                dayName = "Monday";
                break;
            case 2:
                dayName = "Tuesday";
                break;
            case 3:
                dayName = "Wednesday";
                break;
            case 4:
                dayName = "Thursday";
                break;
            case 5:
                dayName = "Friday";
                break;
            case 6:
            case 7:
                dayName = "Weekend";
                break;
            default:
                dayName = "Invalid day";
                break;
        }
        
        System.out.println("Day: " + dayName);
    }
}

Switch with Strings (Java 7+)

StringSwitch.java
public class StringSwitch {
    public static void main(String[] args) {
        String color = "red";
        String action;
        
        switch (color.toLowerCase()) {
            case "red":
                action = "Stop";
                break;
            case "yellow":
                action = "Caution";
                break;
            case "green":
                action = "Go";
                break;
            default:
                action = "Unknown";
                break;
        }
        
        System.out.println("Action: " + action);
    }
}

Loops

for Loop

ForLoopExample.java
public class ForLoopExample {
    public static void main(String[] args) {
        // Basic for loop
        for (int i = 1; i <= 5; i++) {
            System.out.println("Count: " + i);
        }
        
        // For loop with array
        String[] fruits = {"Apple", "Banana", "Orange"};
        for (int i = 0; i < fruits.length; i++) {
            System.out.println("Fruit: " + fruits[i]);
        }
        
        // Enhanced for loop (for-each)
        for (String fruit : fruits) {
            System.out.println("Enhanced: " + fruit);
        }
    }
}

while Loop

WhileLoopExample.java
public class WhileLoopExample {
    public static void main(String[] args) {
        int count = 1;
        
        // while loop
        while (count <= 5) {
            System.out.println("While count: " + count);
            count++;
        }
        
        // do-while loop
        int number = 0;
        do {
            System.out.println("Do-while: " + number);
            number++;
        } while (number < 3);
    }
}

Break and Continue

BreakContinueExample.java
public class BreakContinueExample {
    public static void main(String[] args) {
        // Break example
        for (int i = 1; i <= 10; i++) {
            if (i == 6) {
                break; // Exit the loop
            }
            System.out.println("Break example: " + i);
        }
        
        // Continue example
        for (int i = 1; i <= 5; i++) {
            if (i == 3) {
                continue; // Skip this iteration
            }
            System.out.println("Continue example: " + i);
        }
    }
}
ℹ️
Use break to exit a loop early and continue to skip to the next iteration.

Learn about methods to organize your code into reusable blocks.

Last updated on