java的break和continue加标签的用法

本文介绍了Java中break和continue语句的区别与用法。详细解释了如何使用这些关键字来控制循环流程,包括如何通过标签指定特定的中断点。

简单来说,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             forint 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             forint 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值