foreach-ui logo
codeLanguages
account_treeDSA

Quick Actions

quizlock Random Quiz
trending_uplock Progress
  • 1
  • 2
  • 3
  • 4
  • quiz
Java
  • Understand conditional statements (if, if-else, switch)
  • Learn different types of loops (for, while, do-while)
  • Master jump statements (break, continue) and best practices

Control Flow Statements in Java

Introduction

Control flow statements in Java allow you to control the execution order of your program. They enable decision-making, looping, and branching based on conditions. Mastering control flow is essential for writing programs that can handle different scenarios and repeat operations efficiently.

Conditional Statements

Conditional statements execute different code blocks based on whether a condition is true or false.

The if Statement

The if statement executes a block of code if a condition is true.

if (condition) {
    // code to execute if condition is true
}
int age = 18;
if (age >= 18) {
    System.out.println("You are eligible to vote.");
}

The if-else Statement

The if-else statement provides an alternative block of code to execute when the condition is false.

if (condition) {
    // code to execute if condition is true
} else {
    // code to execute if condition is false
}
int temperature = 25;
if (temperature > 30) {
    System.out.println("It's hot outside.");
} else {
    System.out.println("The weather is pleasant.");
}

The if-else if-else Ladder

For multiple conditions, use the if-else if-else ladder.

if (condition1) {
    // code for condition1
} else if (condition2) {
    // code for condition2
} else {
    // code if none of the conditions are true
}
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");
}

Nested if Statements

You can nest if statements inside other if statements for complex conditions.

if (condition1) {
    if (condition2) {
        // code when both conditions are true
    }
}

The switch Statement

The switch statement is an alternative to if-else if chains when comparing a single value against multiple constants.

switch (expression) {
    case value1:
        // code for value1
        break;
    case value2:
        // code for value2
        break;
    default:
        // code if no case matches
        break;
}
int day = 3;
String dayName;
switch (day) {
    case 1:
        dayName = "Monday";
        break;
    case 2:
        dayName = "Tuesday";
        break;
    case 3:
        dayName = "Wednesday";
        break;
    default:
        dayName = "Invalid day";
        break;
}
System.out.println("Day: " + dayName);

Looping Statements

Looping statements allow you to execute a block of code repeatedly.

The for Loop

The for loop is used when you know the number of iterations in advance.

for (initialization; condition; update) {
    // code to repeat
}
for (int i = 1; i <= 5; i++) {
    System.out.println("Count: " + i);
}

The while Loop

The while loop executes as long as a condition is true. It's useful when the number of iterations is unknown.

while (condition) {
    // code to repeat
}
int count = 1;
while (count <= 5) {
    System.out.println("Count: " + count);
    count++;
}

The do-while Loop

The do-while loop is similar to while, but it executes the code block at least once before checking the condition.

do {
    // code to repeat
} while (condition);
int number;
do {
    System.out.print("Enter a positive number: ");
    number = scanner.nextInt();
} while (number <= 0);

Jump Statements

Jump statements alter the normal flow of execution.

The break Statement

The break statement terminates the current loop or switch statement.

for (int i = 1; i <= 10; i++) {
    if (i == 5) {
        break;  // exits the loop when i equals 5
    }
    System.out.println(i);
}

The continue Statement

The continue statement skips the current iteration and continues with the next iteration of the loop.

for (int i = 1; i <= 10; i++) {
    if (i % 2 == 0) {
        continue;  // skips even numbers
    }
    System.out.println(i);  // prints only odd numbers
}

Best Practices

  1. Use meaningful variable names and clear conditions.
  2. Avoid deeply nested if statements; consider refactoring into methods.
  3. Always use braces {} even for single-line statements to avoid bugs.
  4. Prefer switch statements for multiple constant comparisons.
  5. Be careful with infinite loops; ensure loop conditions will eventually become false.
  6. Use break and continue sparingly to maintain code readability.
  7. Consider using enhanced for loops (for-each) when iterating over collections.

Common Pitfalls

  1. Forgetting the break statement in switch cases (causes fall-through).
  2. Using = instead of == in conditions (assignment vs. comparison).
  3. Infinite loops due to incorrect loop conditions.
  4. Off-by-one errors in loop bounds.

Summary

Control flow statements are the backbone of program logic in Java. By understanding and properly using conditional statements, loops, and jump statements, you can create programs that make decisions, repeat operations, and handle various scenarios effectively. Practice with different control structures will help you write more robust and flexible Java code.

© 2026 forEach. All rights reserved.

Privacy Policy•Terms of Service