Conditions and loops
if / else, switch expressions, for, the enhanced for, and while.
Runs in Zerethon IDE · Java 25
Official Java documentation ↗
Main.java
public class Main {
public static void main(String[] args) {
for (int n = 1; n <= 4; n++) {
if (n % 2 == 0) {
System.out.println(n + " even");
} else {
System.out.println(n + " odd");
}
}
String[] days = {"SAT", "MON"};
for (String day : days) {
String kind = switch (day) {
case "SAT", "SUN" -> "weekend";
default -> "weekday";
};
System.out.println(day + " is a " + kind);
}
int i = 3;
while (i > 0) {
i--;
if (i == 1) continue;
System.out.println("i = " + i);
}
System.out.println(i == 0 ? "done" : "not yet");
}
}
Output
1 odd 2 even 3 odd 4 even SAT is a weekend MON is a weekday i = 2 i = 0 done
Tip: The arrow form case X -> value does not fall through and can return a value; the old case X: form needs break.