| 7 | |
| 8 | public class BreakAndContinue { |
| 9 | public static void main(String[] args) { |
| 10 | for(int i = 0; i < 100; i++) { // [1] |
| 11 | if(i == 74) break; // Out of for loop |
| 12 | if(i % 9 != 0) continue; // Next iteration |
| 13 | System.out.print(i + " "); |
| 14 | } |
| 15 | System.out.println(); |
| 16 | // Using for-in: |
| 17 | for(int i : range(100)) { // [2] |
| 18 | if(i == 74) break; // Out of for loop |
| 19 | if(i % 9 != 0) continue; // Next iteration |
| 20 | System.out.print(i + " "); |
| 21 | } |
| 22 | System.out.println(); |
| 23 | int i = 0; |
| 24 | // An "infinite loop": |
| 25 | while(true) { // [3] |
| 26 | i++; |
| 27 | int j = i * 27; |
| 28 | if(j == 1269) break; // Out of loop |
| 29 | if(i % 10 != 0) continue; // Top of loop |
| 30 | System.out.print(i + " "); |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | /* Output: |
| 35 | 0 9 18 27 36 45 54 63 72 |