C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Continue: We move to the next iteration of the loop if the value is evenly divisible by 2 or 3. We use a modulo division test.
ModuloResult: We print (with System.out.println) 10 numbers that are not evenly divisible by 2 or 3.
Java program that uses continue keyword
import java.util.Random;
public class Program {
public static void main(String[] args) {
Random random = new Random();
int count = 0;
// Use while-loop until count reaches 10.
while (count < 10) {
// Get random number.
// ... Can be 0 through 9 inclusive.
// ... Bound argument (10) is exclusive.
int value = random.nextInt(10);
// Continue to next iteration if value is divisible by 2.
if (value % 2 == 0) {
continue;
}
// Check for 3.
if (value % 3 == 0) {
continue;
}
// Print result.
System.out.println("Not divisible by 2 or 3: " + value);
count++;
}
}
}
Output
Not divisible by 2 or 3: 7
Not divisible by 2 or 3: 7
Not divisible by 2 or 3: 7
Not divisible by 2 or 3: 1
Not divisible by 2 or 3: 5
Not divisible by 2 or 3: 5
Not divisible by 2 or 3: 1
Not divisible by 2 or 3: 1
Not divisible by 2 or 3: 7
Not divisible by 2 or 3: 1
Java program that causes infinite looping
public class Program {
public static void main(String[] args) {
while (true) {
continue;
}
}
}