🔀 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:
switchandnested switchwhileanddo-whileloopsEnhanced
for-eachloopJump 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
breakstops execution of the switch blockWithout
break, fall-through occursdefaultexecutes when no case matchesdefaultis 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
| Feature | while loop | do-while loop |
| Condition check | Before execution | After execution |
| Minimum runs | 0 times | At 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:
breakcontinuereturn
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)
| Concept | Purpose |
| switch | Multiple fixed choices |
| nested switch | Multi-level decisions |
| while | Unknown iterations |
| do-while | Executes at least once |
| for-each | Array traversal |
| break | Exit loop or switch |
| continue | Skip iteration |
| return | Exit method with value |