首页 技术 正文
技术 2022年11月13日
0 收藏 589 点赞 3,147 浏览 823 个字

  标签 是后面跟有冒号的标识符,格式如下:

    label :

  java中通过break与continue关键词可以完成类似于跳转的操作,其实现机制便是标签。

  虽然很少有人使用,但是其有自身的适用场景:多层嵌套,跳转到指定循环体中。

  练习示例:

public static void main(String[] args) {        int i = 0;        outer:
while(true){
System.out.println("this is the start of outer loop!");
while(true){
i++;
if(i==1){
System.out.println("i="+i);
continue;
}
if(i==3){
System.out.println("i="+i);
break;
}
if(i==5){
System.out.println("i="+i);
continue outer;
}
if(i==7){
System.out.println("i="+i);
break outer;
}
}
System.out.println("this is the end of outer loop!");
}
}

输出结果:

this is the start of outer loop!   //outer循环第一次开始
i=1
i=3 //continue继续下次当前循环
this is the end of outer loop! //break跳出内部循环,执行到外部循环的底部
this is the start of outer loop! //继续第二次外部循环
i=5
this is the start of outer loop! //i=5时,continue+标签名称,直接跳出内部循环,去执行第三次外部循环,再次打印外部循环开始部分
i=7 //此时,break+标签名称,直接跳出外部循环,执行结束。

确实,标签的使用很少见,目前个人接触的项目中也并没有出现,但是对于复杂逻辑判断,设计多层循环嵌套时,不失为循环跳转的一种解决方案。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,093
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,570
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,417
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,190
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,825
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,908