Junior — Middle
What types of cycles have you used in Java programming?
sobes.tech AI
Answer from AI
In Java, I used several types of loops:
- for — a classic loop with initialization, condition, and increment, suitable for iterating a known number of times.
for (int i = 0; i < 10; i++) {
System.out.println(i);
}
- while — executes as long as the condition is true, suitable when the number of iterations is unknown in advance.
int i = 0;
while (i < 10) {
System.out.println(i);
i++;
}
- do-while — similar to while, but the loop body executes at least once.
int i = 0;
do {
System.out.println(i);
i++;
} while (i < 10);
- foreach (enhanced for) — convenient for iterating over elements of collections and arrays.
int[] arr = {1, 2, 3};
for (int num : arr) {
System.out.println(num);
}