2015-09-07 11:44:36 -06:00
|
|
|
// control/BreakAndContinue.java
|
2016-12-30 17:23:13 -08:00
|
|
|
// (c)2017 MindView LLC: see Copyright.txt
|
2015-11-15 15:51:35 -08:00
|
|
|
// We make no guarantees that this code is fit for any purpose.
|
2016-09-23 13:23:35 -06:00
|
|
|
// Visit http://OnJava8.com for more book information.
|
2016-01-25 18:05:55 -08:00
|
|
|
// Break and continue keywords
|
2015-11-11 20:20:04 -08:00
|
|
|
import static onjava.Range.*;
|
2015-06-15 17:47:35 -07:00
|
|
|
|
|
|
|
public class BreakAndContinue {
|
|
|
|
public static void main(String[] args) {
|
2016-11-21 12:37:57 -08:00
|
|
|
for(int i = 0; i < 100; i++) { // [1]
|
2015-06-15 17:47:35 -07:00
|
|
|
if(i == 74) break; // Out of for loop
|
|
|
|
if(i % 9 != 0) continue; // Next iteration
|
2015-12-15 11:47:04 -08:00
|
|
|
System.out.print(i + " ");
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
|
|
|
System.out.println();
|
2015-11-11 20:20:04 -08:00
|
|
|
// Using for-in:
|
2016-11-21 12:37:57 -08:00
|
|
|
for(int i : range(100)) { // [2]
|
2015-06-15 17:47:35 -07:00
|
|
|
if(i == 74) break; // Out of for loop
|
|
|
|
if(i % 9 != 0) continue; // Next iteration
|
2015-12-15 11:47:04 -08:00
|
|
|
System.out.print(i + " ");
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
|
|
|
System.out.println();
|
|
|
|
int i = 0;
|
|
|
|
// An "infinite loop":
|
2016-11-21 12:37:57 -08:00
|
|
|
while(true) { // [3]
|
2015-06-15 17:47:35 -07:00
|
|
|
i++;
|
|
|
|
int j = i * 27;
|
|
|
|
if(j == 1269) break; // Out of loop
|
|
|
|
if(i % 10 != 0) continue; // Top of loop
|
2015-12-15 11:47:04 -08:00
|
|
|
System.out.print(i + " ");
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
|
|
|
}
|
2015-09-07 11:44:36 -06:00
|
|
|
}
|
|
|
|
/* Output:
|
2015-06-15 17:47:35 -07:00
|
|
|
0 9 18 27 36 45 54 63 72
|
|
|
|
0 9 18 27 36 45 54 63 72
|
|
|
|
10 20 30 40
|
2015-09-07 11:44:36 -06:00
|
|
|
*/
|