for (int i = 0; i < 5; i++) {
System.out.print(i);
if (i == 3) {
break;
}
}
// Outputs: 0123
Java Loops
Break Statement
Continue Statement
for (int i = 0; i < 5; i++) {
if (i == 3) {
continue;
}
System.out.print(i);
}
// Outputs: 01245
Do While Loop
int count = 0;
do {
System.out.print(count);
count++;
} while (count < 5);
// Outputs: 01234
While Loop
int count = 0;
while (count < 5) {
System.out.print(count);
count++;
}
// Outputs: 01234
Enhanced For Loop
int[] numbers = {1,2,3,4,5};
for (int number: numbers) {
System.out.print(number);
}
// Outputs: 12345
Used to loop around array's or List's
For Loop
for (int i = 0; i < 10; i++) {
System.out.print(i);
}
// Outputs: 0123456789
for (int i = 0,j = 0; i < 3; i++,j--) {
System.out.print(j + "|" + i + " ");
}
// Outputs: 0|0 -1|1 -2|2
Comments