Every Java program is a sequence of statements — but selection, iteration, and jump statements are what let it branch, repeat, and change direction. Here's how each one behaves, with runnable examples.
PART 01
Selection Statements
Selection statements let a program choose which block of code to run based on a condition — the code branches instead of running top to bottom.
1.1
if statement
Runs a block only when its condition evaluates to true. If the condition is false, the block is skipped entirely.
age >= 18→ true →print "Eligible to vote"→ continue program
IfDemo.javaJDK 17
1
2
3
4
5
6
7
8
publicclassIfDemo {
publicstaticvoidmain(String[] args) {
int age = 20;
if (age >= 18) {
System.out.println("Eligible to vote");
}
}
}
Console output$Eligible to vote
1.2
if-else statement
Adds a fallback: one block runs when the condition is true, the other runs when it's false. Exactly one branch always executes.
num % 2 == 0true →"Even"false →"Odd"
IfElseDemo.javaJDK 17
1
2
3
4
5
6
7
8
9
10
publicclassIfElseDemo {
publicstaticvoidmain(String[] args) {
int num = 7;
if (num % 2 == 0) {
System.out.println(num + " is Even");
} else {
System.out.println(num + " is Odd");
}
}
}
Console output$7 is Odd
1.3
else-if ladder
Chains several conditions in order. Java tests them top to bottom and runs the first one that's true — everything after that is skipped.
Note: Since 76 satisfies marks >= 75, Java stops there — it never checks marks >= 60, even though that's also true.
1.4
switch statement
Matches one variable against several constant values. Cleaner than a long else-if ladder when you're checking a single value against many possibilities. Don't forget break — without it, execution "falls through" into the next case.
DayDemo.javaJDK 17
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
publicclassDayDemo {
publicstaticvoidmain(String[] args) {
int day = 3;
String name;
switch (day) {
case1: name = "Monday"; break;
case2: name = "Tuesday"; break;
case3: name = "Wednesday"; break;
case4: name = "Thursday"; break;
case5: name = "Friday"; break;
default: name = "Weekend"; break;
}
System.out.println("Day: " + name);
}
}
Console output$Day: Wednesday
Modern tip: Since Java 14, arrow-style switch (case 3 -> "Wednesday";) skips the fall-through problem entirely, since each branch is isolated by default.
PART 02
Iteration Statements
Iteration statements — loops — repeat a block of code while a condition holds, so you don't have to write the same lines over and over.
2.1
for loop
Best when you know how many times you want to repeat. It bundles initialization, condition, and update into one line: for (init; condition; update).
i=1i=2i=3i=4i=5i=6 → stop
condition i <= 5 checked before every iteration; update i++ runs after each pass
ForDemo.javaJDK 17
1
2
3
4
5
6
7
publicclassForDemo {
publicstaticvoidmain(String[] args) {
for (int i = 1; i <= 5; i++) {
System.out.println("i = " + i);
}
}
}
Console output$i = 1 $i = 2 $i = 3 $i = 4 $i = 5
2.2
while loop
Checks the condition before each iteration. Best when you don't know the exact number of repetitions in advance — the loop can run zero times if the condition starts out false.
Checks the condition after each iteration, so the body always runs at least once — even if the condition is false from the start.
DoWhileDemo.javaJDK 17
1
2
3
4
5
6
7
8
9
publicclassDoWhileDemo {
publicstaticvoidmain(String[] args) {
int num = 10;
do {
System.out.println("num = " + num);
num++;
} while (num < 5);
}
}
Console output$num = 10
Note: Even though num < 5 is false immediately (10 is not less than 5), the body still runs once before the condition is ever checked — that's the defining trait of do-while.
2.4
for-each (enhanced for) loop
Walks through every element of an array or collection without needing an index variable. Cleaner and safer when you just need each value, not its position.
Jump statements interrupt the normal flow of a loop or method — to exit early, skip ahead, or hand back a result.
3.1
break statement
Immediately exits the nearest enclosing loop or switch. Nothing after break in that iteration runs, and the loop doesn't continue.
i=1i=2i=3i=4 → breaki=5i=6
loop exits the moment i == 4 — the remaining iterations never run
BreakDemo.javaJDK 17
1
2
3
4
5
6
7
8
9
publicclassBreakDemo {
publicstaticvoidmain(String[] args) {
for (int i = 1; i <= 6; i++) {
if (i == 4) {
break; // exit the loop entirely
}
System.out.println("i = " + i);
}
}
}
Console output$i = 1 $i = 2 $i = 3
3.2
continue statement
Skips the rest of the current iteration and jumps straight to the loop's next check — the loop keeps running, it just skips one pass.
i=1i=2i=3 → skippedi=4i=5
continue fires when i == 3, so that pass prints nothing — the loop still finishes
ContinueDemo.javaJDK 17
1
2
3
4
5
6
7
8
9
publicclassContinueDemo {
publicstaticvoidmain(String[] args) {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue; // skip this iteration only
}
System.out.println("i = " + i);
}
}
}
Console output$i = 1 $i = 2 $i = 4 $i = 5
3.3
return statement
Exits the current method immediately, optionally handing a value back to whoever called it. Any code written after return in that method never executes.
ReturnDemo.javaJDK 17
1
2
3
4
5
6
7
8
9
10
11
publicclassReturnDemo {
staticintsquare(int n) {
return n * n; // hands the value back and exits
}
publicstaticvoidmain(String[] args) {
int result = square(6);
System.out.println("Result: " + result);
}
}