简单来说,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 }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 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 }
转载于:https://www.cnblogs.com/yelcoved/archive/2013/02/06/2908236.html
