Junior — Middle
In what case should the continue operator be used in a loop and how does it affect program execution?
sobes.tech AI
Answer from AI
The continue operator in a loop is used to skip the current iteration and move to the next one. This is useful when, under certain conditions, you want to skip the remaining code inside the loop for the current step.
For example, if you only want to process positive numbers in an array, you can skip negative ones using continue:
for (int num : numbers) {
if (num < 0) {
continue; // skip negative numbers
}
System.out.println(num); // process only positive numbers
}
Thus, continue helps make the code cleaner and avoid nested conditions, improving readability and execution logic.