Java Basics #3 — Control Flow: If, Switch & Loops
Conditionals, the new switch expressions, for/while loops, and patterns that make your code cleaner.
Series
java-basics-to-advanced
If / Else If / Else
int score = 85;
if (score >= 90) {
System.out.println("A");
} else if (score >= 80) {
System.out.println("B"); // this runs
} else if (score >= 70) {
System.out.println("C");
} else {
System.out.println("F");
}
Ternary operator — for simple one-liners:
String result = score >= 60 ? "Pass" : "Fail";
A single ternary is readable. Nested ternaries like a ? b : c ? d : e are not. Use if/else when logic gets complex.
Switch — Old vs New Style
Old switch (statement) — still works, but error-prone:
int day = 3;
switch (day) {
case 1:
System.out.println("Monday");
break; // ⚠ forget this and it falls through to next case
case 2:
System.out.println("Tuesday");
break;
default:
System.out.println("Other");
}
New switch expression (Java 14+) — use this instead:
String dayName = switch (day) {
case 1 -> "Monday";
case 2 -> "Tuesday";
case 3 -> "Wednesday";
case 4, 5 -> "Thu or Fri"; // multiple labels
default -> "Weekend";
};
Switch with blocks (for multi-line logic):
int result = switch (day) {
case 1, 2, 3, 4, 5 -> {
System.out.println("Weekday");
yield day * 2; // yield returns a value from a block
}
default -> 0;
};
Switch on Strings (Java supports this, unlike C/C++):
String role = "ADMIN";
String access = switch (role) {
case "ADMIN" -> "Full access";
case "USER" -> "Read-only";
case "GUEST" -> "Public only";
default -> "No access";
};
For Loop
// Classic for loop
for (int i = 0; i < 5; i++) {
System.out.println(i); // 0, 1, 2, 3, 4
}
// Backwards
for (int i = 4; i >= 0; i--) {
System.out.println(i); // 4, 3, 2, 1, 0
}
// Step by 2
for (int i = 0; i <= 10; i += 2) {
System.out.println(i); // 0, 2, 4, 6, 8, 10
}
For-Each Loop — Use This for Collections
int[] numbers = {10, 20, 30, 40, 50};
for (int num : numbers) {
System.out.println(num);
}
// Works on any Iterable
List<String> names = List.of("Alice", "Bob", "Charlie");
for (String name : names) {
System.out.println(name);
}
When you don't need the index, for-each is cleaner and less error-prone. Use the classic for only when you actually need i.
While & Do-While
// while — checks condition BEFORE executing
int count = 0;
while (count < 3) {
System.out.println(count); // 0, 1, 2
count++;
}
// do-while — executes ONCE then checks condition
int x = 10;
do {
System.out.println(x); // prints 10 even though condition is false immediately
x++;
} while (x < 5);
Break & Continue
// break — exits the loop entirely
for (int i = 0; i < 10; i++) {
if (i == 5) break;
System.out.println(i); // 0, 1, 2, 3, 4
}
// continue — skips the current iteration
for (int i = 0; i < 10; i++) {
if (i % 2 == 0) continue; // skip even numbers
System.out.println(i); // 1, 3, 5, 7, 9
}
Labeled break — for breaking out of nested loops:
outer:
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (i == 1 && j == 1) break outer; // exits both loops
System.out.println(i + ", " + j);
}
}
Putting It Together — A Real Example
public class FizzBuzz {
public static void main(String[] args) {
for (int i = 1; i <= 20; i++) {
String output = switch (0) {
case 0 when i % 15 == 0 -> "FizzBuzz";
case 0 when i % 3 == 0 -> "Fizz";
case 0 when i % 5 == 0 -> "Buzz";
default -> String.valueOf(i);
};
System.out.println(output);
}
}
}
Java 21 adds pattern matching to switch — you can match on types and conditions together. We'll cover this in the Advanced Java section.
What's Next?
In Java Basics #4 we cover methods and arrays — how to structure reusable logic and work with fixed-size data.
✦ Enjoyed this post?
Get posts like this in your inbox
No spam, just real tutorials when they're ready.
Discussion
Powered by GitHubComments use GitHub Discussions — no separate account needed if you have GitHub.