Spaces:
Running
Running
| it jumps back to the start line of the loop and the condition clause is evaluated again | |
| when using the for loop, the CHANGE part is executed again BEFORE the evaluation of the CONDITION clause | |
| VS | |
| but when using the WHILE clause, continue can easily lead to a perpetual loop. | |
| The following example attempts to print squares of even values. | |
| However, the program remains in a perpetual loop: the continue statement aborts the loop, so the value of the number variable is not changed either. | |
| public class Example { | |
| public static void main(String[] args) { | |
| // Program tries to calculate the squares of | |
| // even elements | |
| int num = 1; | |
| while (num <= 30) { | |
| // This causes an INFINITE LOOP, because | |
| // variable num value never grows after | |
| // the first odd value | |
| if (luku % 2 == 1) { | |
| continue; | |
| } | |
| int square = num * num; | |
| System.out.println("The square of the number is: " + square); | |
| num++; | |
| } | |
| } | |
| } | |