语法:
continue 标号名
break 标号名
作用:
continue 标号名 : continue 后的标号名 应该标识 在外层循环语句 ,其作用是使 程序的流程 转入标识的循环层次,继续执行
break 标号名 : break 跳出 这个标识的语块,执行该语句的后续语句;
代码:
public class ContinueWithLableExample {
public static void main(String[] args) {
String searchMe="Look for a substring in me";
String substring="for";
boolean foundIt=false;
int max=searchMe.length()-substring.length();
System.out.println(searchMe.length());
test:
for(int i=0;i<=max;i++){
int n =substring.length();
System.out.println(n);
int j=i;
int k=0;
System.out.println(k);
while(n-- !=0){
if(searchMe.charAt(j++)!=substring.charAt(k++)){
System.out.println("j= "+j);
System.out.println("k= "+k);
continue test;
}
}
foundIt=true;
break test;
}
System.out.println(foundIt?"FoudIt":"Didn't find it");
}
}
输出结果:
26
3
0
j= 1
k= 1
3
0
j= 2
k= 1
3
0
j= 3
k= 1
3
0
j= 4
k= 1
3
0
j= 5
k= 1
3
0
FoudIt
分析这串代码的运行轨迹:
1:代码都是顺序执行;
2:此处是双层循环,有嵌套的continue 和break语句;
当
while(n-- !=0){
if(searchMe.charAt(j++)!=substring.charAt(k++)){
System.out.println("j= "+j);
System.out.println("k= "+k);
continue test;
}
}
当if
if(searchMe.charAt(j++)!=substring.charAt(k++)){
System.out.println("j= "+j);
System.out.println("k= "+k);
continue test;
}
当if中的条件为真 则 continue test,一次一次的跳出
|
.
.
.
循环
.
.
.
|
当循环到 第 六 次;
即:int j=5
n=3
j++ 为6
k++ 为1
此时if中的条件为false; 就会运行while(n-- !=0){}的循环
n=2;
j++ 为7;
k++为2
此时if中条件为false;任然运行while(n-- !=0){}的循环
n=1;
j++ 为8;
k++为3;
此时if中条件为false,运行while(n-- !=0){}此时n=0 跳出while循环执行
foundIt=true;
break test;
跳出test了
此时 foundIt = true.
分析:
n循环的次数 和 substring的length(),这样就判定了 substring 是否与string中的匹配;
另外 在同一个循环中 变量是不断的赋值和赋值,j and k 是一个局部变量
分享到:
相关推荐
Idiot‘s_Guides_to_Beginning_Programming.pdf
An Idiot's Guide to C++ Templates
Idiot
The Complete Idiot’s Guide to iPad and iPhone App Development .pdf
The Complete Idiot's Guide to Improving Your IQ.pdf
傻瓜指南系列-如何作小生意]The.Complete.Idiot's.Guide.-.Small.Business.pdf
Explorer扩展编程的指导资料,包括一个chm教程以及其对应的源代码。 Part I - A step-by-step tutorial on writing shell extensions Part II - A tutorial on writing a shell extension that operates on ...
idiot
详细说明Windows Shell的编程指南,可以处理Windows Explorer资源管理器很多的交互,比如文件复制和剪切,鼠标右键菜单的添加,任意文件的图标的修改和叠加,系统文件拷贝进度条的控制等等,文件属性自定义扩展页和...
An Idiot’s guide to Support vector machines (SVMs)
加密已有文件,可以临时加密和永久解密;加密特定文件夹
This guide helps environmentally conscious people make real-world ...·More than 80 regional and local green building programs are in place in the U.S. and more are being promoted in the private sector
HTML Reference for dummies, idiot, beginner
most simple adding for those idiot
CSS 2 Reference for begginer, idiot, dummies
American Idiot is the seventh studio album by American rock band Green Day
笨蛋 物联网ID
这是关于Java基础的入门教程,主要是数据结构,txt版的
Jnario是用于编写Java可执行规范的框架。 它提供了Xtend的所有优势,同时专门为编写验收和单元测试而量身定制。 当Jnario规范被编译为普通Java Junit测试时,很容易集成。 请访问了解更多信息。 请参阅当前版本的。...