Loops

for

for (int i = 0; i < 5; i++) {
    System.out.println("Iteration " + i);
}

for each

List<Integer> integerList = new ArrayList<>();
integerList.add(1);
integerList.add(2);
integerList.add(3);

for (Integer integer : integerList) {
    System.out.println(integer);
}

while

int count = 0;
while (count < 3) {
    System.out.println("Count: " + count);
    count++;
}

do-while

same as while loop but guarantees at least one execution of the block of code.

break

continue

Last updated