while (condition) { }do { } while (condition);: runs at least oncefor (int i = 0; i < 5; i++) { }for (String s : array) { }: the for-each loop
break exits a loop; continue skips to the next iteration.
while, do-while, for and for-each.
✍️ 2 exercises in this lessonwhile (condition) { }do { } while (condition);: runs at least oncefor (int i = 0; i < 5; i++) { }for (String s : array) { }: the for-each loopbreak exits a loop; continue skips to the next iteration.
Mastering Java Loops helps you write clean and reliable Java code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.
public class Main {
public static void main(String[] args) {
for (int i = 1; i <= 3; i++) {
System.out.println("9 x " + i + " = " + 9 * i);
}
int n = 3;
while (n > 0) {
System.out.println("Countdown " + n);
n--;
}
String[] langs = {"Java", "Kotlin", "Scala"};
for (String lang : langs) {
if (lang.equals("Kotlin")) continue;
System.out.println(lang);
}
}
}
9 x 1 = 9 9 x 2 = 18 9 x 3 = 27 Countdown 3 Countdown 2 Countdown 1 Java Scala
Check what you learned in “Java Loops”.
Loop over every element (for-each).
for (String s names) { }
Which keyword exits a loop early?
Stuck on something?
Ask the community in the CodesInTech forum.
Need a developer?
Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.
Explore Our Services