Skip to main content

Command Palette

Search for a command to run...

🔀 Switch, Loops and Jump Statements in Java (Complete Beginner Guide)

Published
4 min read
🔀 Switch, Loops and Jump Statements in Java (Complete Beginner Guide)

Java control statements form the backbone of decision-making and looping logic.
If you understand these clearly, 70% of Java basics becomes easy.

In this blog, we’ll cover:

  • switch and nested switch

  • while and do-while loops

  • Enhanced for-each loop

  • Jump statements: break, continue, return

Each concept includes syntax, examples, output and interview tips.


🔀 Switch Statement in Java

What is a switch statement?

The switch statement is used to select one execution path from multiple fixed choices, based on the value of an expression.

It is an alternative to long if-else if ladders.


Syntax

switch (expression) {
    case value1:
        // statements
        break;
    case value2:
        // statements
        break;
    default:
        // statements
}

Example

int day = 2;

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

Output

Tuesday

Important Rules

  • break stops execution of the switch block

  • Without break, fall-through occurs

  • default executes when no case matches

  • default is optional but recommended


🔁 Nested Switch Statement

What is a nested switch?

A switch inside another switch is called a nested switch.

When to use?

  • Multi-level decision making

  • Menu-driven programs

  • Console-based applications


Example

int option = 1;
int subOption = 2;

switch (option) {
    case 1:
        switch (subOption) {
            case 1:
                System.out.println("Add");
                break;
            case 2:
                System.out.println("Edit");
                break;
        }
        break;
    default:
        System.out.println("Invalid");
}

Output

Edit

🔄 While Loop in Java

What is a while loop?

The while loop checks the condition before execution.

If the condition is false initially, the loop may not run at all.


Syntax

while (condition) {
    // statements
}

Example

int i = 1;

while (i <= 3) {
    System.out.println(i);
    i++;
}

Output

1
2
3

Best Use Case

  • When the number of iterations is unknown

  • Input-driven loops (user input, file reading)


🔂 Do-While Loop in Java

What is a do-while loop?

The do-while loop checks the condition after execution.
So, the loop executes at least once, even if the condition is false.


Syntax

do {
    // statements
} while (condition);

Example

int i = 5;

do {
    System.out.println(i);
    i++;
} while (i <= 3);

Output

5

While vs Do-While

Featurewhile loopdo-while loop
Condition checkBefore executionAfter execution
Minimum runs0 timesAt least once

🔁 Enhanced For-Each Loop

What is for-each loop?

The enhanced for-each loop is used to traverse arrays and collections easily.

It improves readability and reduces boilerplate code.


Syntax

for (datatype variable : array) {
    // statements
}

Example

int[] a = {10, 20, 30};

for (int x : a) {
    System.out.println(x);
}

Output

10
20
30

Limitations

  • ❌ No index access

  • ❌ Cannot modify array elements directly


⛔ Jump Statements in Java

Jump statements are used to alter the normal flow of execution.

Java provides three jump statements:

  • break

  • continue

  • return


1️⃣ break Statement

Used to exit a loop or switch block immediately.

for (int i = 1; i <= 5; i++) {
    if (i == 3)
        break;
    System.out.println(i);
}

Output

1
2

2️⃣ continue Statement

Used to skip the current iteration and move to the next loop cycle.

for (int i = 1; i <= 5; i++) {
    if (i == 3)
        continue;
    System.out.println(i);
}

Output

1
2
4
5

3️⃣ return Statement

Used to exit a method and optionally return a value.

static int add(int a, int b) {
    return a + b;
}

🧠 Quick Comparison Table (Exam & Interview Ready)

ConceptPurpose
switchMultiple fixed choices
nested switchMulti-level decisions
whileUnknown iterations
do-whileExecutes at least once
for-eachArray traversal
breakExit loop or switch
continueSkip iteration
returnExit method with value