There are about 4 loops in Java.
For loop
class Main { public static void main(String[] args) { for (int number = 1; number <= 10; number++) { System.out.println(number); } // outputs 1 to 10 }}- Initialize a variable
- Check against a condition
- Do something if the condition is true (print the number)
- After executing the loop body, do the update
Using a for loop to loop over an array
class Main { public static void main(String[] args) { int numbers[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
for (int index = 0; index < numbers.length; index++) { System.out.println((numbers[index])); } }}We can also use the loop to find the sum of all the numbers in the array.
class Main { public static void main(String[] args) { int numbers[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int sum = 0;
for (int index = 0; index < numbers.length; index++) { sum += numbers[index]; } System.out.println(sum); }}We can print out multiplication tables of any number using nested for loops.
class Main { public static void main(String[] args) { for (int number = 1; number <= 10; number++) { for (int multiplier = 1; multiplier <= 10; multiplier++) { System.out.printf("%d x %d = %d \n", number, multiplier, number * multiplier); } } }}There’s another version of the for loop that makes looping over collections like arrays very easy.
class Main { public static void main(String[] args) { int numbers[] = {1, 2, 3, 4, 5};
for (int number : numbers) { System.out.println(number); } }}For every integer number in the numbers array, we print out that number. Note that the int number in the for loop matches the type int of the numbers array.
While loop
class Main { public static void main(String[] args) { int number = 5; int multiplier = 1;
while (multiplier <= 10) { System.out.printf("%d x %d = %d \n", number, multiplier, number * multiplier);
multiplier++; } }}The difference between a while and a for loop is that the for loop has the initialization,
condition, loop body, and update all in one place. A while loop only has the condition — the
initialization should be done before the loop, and the update inside the loop body.
Do-while
Works similarly to a while loop but differently in one respect.
class Main { public static void main(String[] args) { int number = 5; int multiplier = 1;
do { System.out.printf("%d x %d = %d \n", number, multiplier, number * multiplier);
multiplier++; } while (multiplier <= 10); }}The main difference from a regular while loop is that in a do-while loop, the loop body executes first, and then the condition is checked — the other way round from a while loop.
For-each
public class Main { public static void main(String[] args) { String[] fruits = {"Apple", "Banana", "Cacao"};
for (String fruit : fruits) { System.out.println(fruit); } }}For each fruit in the fruits array, we print out that fruit. The limitation of the for-each
loop is that it only works forward — you can’t print each fruit from last to first like you can
with a normal loop. You also don’t get access to the index of each element.