ControlFlow.java

Java Control Statements

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
public class IfDemo { public static void main(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 == 0 true → "Even" false → "Odd"
IfElseDemo.javaJDK 17
1 2 3 4 5 6 7 8 9 10
public class IfElseDemo { public static void main(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.

GradeDemo.javaJDK 17
1 2 3 4 5 6 7 8 9 10 11 12 13
public class GradeDemo { public static void main(String[] args) { int marks = 76; if (marks >= 90) { System.out.println("Grade: A"); } else if (marks >= 75) { System.out.println("Grade: B"); } else if (marks >= 60) { System.out.println("Grade: C"); } else { System.out.println("Grade: F"); } } }
Console output$ Grade: B
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
public class DayDemo { public static void main(String[] args) { int day = 3; String name; switch (day) { case 1: name = "Monday"; break; case 2: name = "Tuesday"; break; case 3: name = "Wednesday"; break; case 4: name = "Thursday"; break; case 5: 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
public class ForDemo { public static void main(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.

WhileDemo.javaJDK 17
1 2 3 4 5 6 7 8
public class WhileDemo { public static void main(String[] args) { int count = 1; while (count <= 5) { System.out.println("count = " + count); count++; } } }
Console output $ count = 1
$ count = 2
$ count = 3
$ count = 4
$ count = 5
2.3

do-while loop

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
public class DoWhileDemo { public static void main(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.

ForEachDemo.javaJDK 17
1 2 3 4 5 6 7 8
public class ForEachDemo { public static void main(String[] args) { int[] scores = { 85, 92, 78, 64 }; for (int score : scores) { System.out.println("Score: " + score); } } }
Console output $ Score: 85
$ Score: 92
$ Score: 78
$ Score: 64
PART 03

Jump Statements

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
public class BreakDemo { public static void main(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
public class ContinueDemo { public static void main(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
public class ReturnDemo { static int square(int n) { return n * n; // hands the value back and exits } public static void main(String[] args) { int result = square(6); System.out.println("Result: " + result); } }
Console output$ Result: 36

break vs continue vs return

StatementEffectScope of effect
breakExits the loop/switch immediatelyNearest enclosing loop or switch
continueSkips to the next iterationNearest enclosing loop
returnExits the entire method, optionally with a valueWhole method, even inside nested loops