簡單來說,break,就是中斷,不管你是在做什么,都是停的,continue就是說,跳過這一種情況,然后繼續,就可以了,然后還有更奇葩的事,就是加上標簽的唯一區別原來是從上一個中斷或者跳出,但是現在是根據標簽來跳出,或者中斷,因此,根據標簽頁,不再根據上一個循環了,然后break的label是必須帶有花括號的,然而,continue是不能帶有花括號的,這是唯一的區別,否則就會出現語法錯誤,附上代碼
1
package exercise;
2
3 import javax.swing.JOptionPane;
4
5 public class ch5_2 {
6 public static void main(String[] args) {
7 String output = "";
8
9 stop:{
10
11 for( int row = 1;row <= 10;row++){
12
13 for( int column = 1;column <= 5;column ++){
14 if( row == 5)
15 break stop; // 此時的break直接跳出stop帶標記的,可以指定怎么跳出
16 output+="*";
17
18 }
19 output+="\n";
20
21 }
22 output += "\nLoops terminated normally";
23
24 }
25
26 JOptionPane.showMessageDialog( null, output,
27 "Testing break with a label ",JOptionPane.INFORMATION_MESSAGE);
28 }
2
3 import javax.swing.JOptionPane;
4
5 public class ch5_2 {
6 public static void main(String[] args) {
7 String output = "";
8
9 stop:{
10
11 for( int row = 1;row <= 10;row++){
12
13 for( int column = 1;column <= 5;column ++){
14 if( row == 5)
15 break stop; // 此時的break直接跳出stop帶標記的,可以指定怎么跳出
16 output+="*";
17
18 }
19 output+="\n";
20
21 }
22 output += "\nLoops terminated normally";
23
24 }
25
26 JOptionPane.showMessageDialog( null, output,
27 "Testing break with a label ",JOptionPane.INFORMATION_MESSAGE);
28 }
29 }
1
package exercise;
2
3 import javax.swing.JOptionPane;
4
5 public class ch5_3 {
6
7 public static void main(String[] args) {
8 String output = "";
9 nextRow: // continue不加花括號,break則加
10
11 for( int row = 1;row <= 5;row++){
12 output+="\n";
13
14 for( int column = 1;column <= 10;column++){
15
16 if(column > row)
17 continue nextRow;
18
19 output += "* ";
20
21 }
22 }
23 JOptionPane.showMessageDialog( null, output,
24 "Testing continue with label",JOptionPane.INFORMATION_MESSAGE);
25 }
26
2
3 import javax.swing.JOptionPane;
4
5 public class ch5_3 {
6
7 public static void main(String[] args) {
8 String output = "";
9 nextRow: // continue不加花括號,break則加
10
11 for( int row = 1;row <= 5;row++){
12 output+="\n";
13
14 for( int column = 1;column <= 10;column++){
15
16 if(column > row)
17 continue nextRow;
18
19 output += "* ";
20
21 }
22 }
23 JOptionPane.showMessageDialog( null, output,
24 "Testing continue with label",JOptionPane.INFORMATION_MESSAGE);
25 }
26
1
package exercise;
2
3 import javax.swing.JOptionPane;
4
5 public class ch5_3 {
6
7 public static void main(String[] args) {
8 String output = "";
9 nextRow: // continue不加花括號,break則加
10
11 for( int row = 1;row <= 5;row++){
12 output+="\n";
13
14 for( int column = 1;column <= 10;column++){
15
16 if(column > row)
17 continue nextRow;
18
19 output += "* ";
20
21 }
22 }
23 JOptionPane.showMessageDialog( null, output,
24 "Testing continue with label",JOptionPane.INFORMATION_MESSAGE);
25 }
2
3 import javax.swing.JOptionPane;
4
5 public class ch5_3 {
6
7 public static void main(String[] args) {
8 String output = "";
9 nextRow: // continue不加花括號,break則加
10
11 for( int row = 1;row <= 5;row++){
12 output+="\n";
13
14 for( int column = 1;column <= 10;column++){
15
16 if(column > row)
17 continue nextRow;
18
19 output += "* ";
20
21 }
22 }
23 JOptionPane.showMessageDialog( null, output,
24 "Testing continue with label",JOptionPane.INFORMATION_MESSAGE);
25 }
26 }