Home > Java > javaTutorial > body text

Detailed explanation of the use of break&continue keywords

零下一度
Release: 2017-06-29 11:55:46
Original
1386 people have browsed it

Use of break&continue keywords

break: Used in switch...case statements or loop structure statements to end the current loop.

Sample code:

 1 public class TestBreak { 2     public static void main(String[] args) { 3         for(int i = 1; i <= 5; i++){ 4             if(i % 4 == 0){ 5                 break;//如果满足i对4取余为零,即i可以被4整除时执行break关键字,跳出循环,后续语句均不执行,在这个循环里i最大值为5,所以这里只有4可以被4整除所以打印语句只会打印1-3的值 6             } 7             System.out.println("i="+i); 8         } 9     }10 }
Copy after login

Use the break keyword in nested loops:

 1 public class TestBreak { 2     public static void main(String[] args) { 3         //两层循环 4         for(int i = 1; i <= 5; i++){ 5             for(int j = 1; j <= 5; j++){ 6                 if(j % 4 == 0){ 7                     break; //由于是两层循环,而break关键字使用在内层循环,如果满足条件,则只会跳出内层循环,再次进入外层循环执行语句 8                 } 9                 System.out.print("j="+j+"\t");10                 //所以会打印外层循环规定的次数的J的值,但依旧不会打印4之后的数字11             }12             System.out.println();13         }14     }15 }
Copy after login

continue: Used in loop structure statements to indicate the end of the current loop.

Sample code:

 1 public class TestContinue { 2     public static void main(String[] args) { 3         //需要和break关键字区分开,所以讲循环条件改为10,可以更清晰的看出break和continue的区别 4         for(int i = 1; i <= 10; i++){ 5             if(i % 4 == 0){ 6                 continue;//如果满足i对4取余为零,即i可以被4整除时执行continue关键字,结束本次循环,本次循环的后续语句均不执行,但下一次的循环语句若不满足被4整除的条件则会照常执行 7             } 8             System.out.print("i="+i+"\t"); 9         }10         //运行后会发现有两个数字没有打印,但是后续不满足该条件的却都打印出来,和break关键字截然不同11     }12 }
Copy after login

Use the continue keyword in nested loops:

 1 public class TestContinue { 2     public static void main(String[] args) { 3         //两层循环 4         for(int i = 1; i <= 5; i++){ 5             for(int j = 1; j <= 10; j++){ 6                 if(j % 4 == 0){ 7                     continue; //由于是两层循环,而continue关键字使用在内层循环,如果满足条件,则只会结束本次内层循环,执行下一次内层循环语句 8                 } 9                 System.out.print("j="+j+"\t");10                 //所以会打印外层循环规定的次数的J的值,但不会打印能够被4整除的数字11             }12             System.out.println();13         }14     }15 }
Copy after login

Supplementary information:

Both break and continue have a newly added function. When performing multi-layer nested loops, if you want to use the break and continue keywords to end a loop that is not the current layer but a certain layer, you can follow the keywords. Add a label. The label name can be named by yourself, such as English label

. At the same time, you also need to add a label in front of the for keyword of the loop layer you want to end:, Also use label as an example - label:for(int i=0; loop condition; iteration){}.

The above is the detailed content of Detailed explanation of the use of break&continue keywords. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template