[Java process control learning] break and continue usage

break

Used to forcibly exit the loop without executing the remaining statements in the loop. (The break statement is also used in the switch statement)

int t=0;
while (t<90){
    
    
    t++;
    System.out.println(t);
    if (t==5){
    
    
        break;
    }
/*输出结果:
1
2
3
4
5
*/

contain

Used to terminate a cycle process, and then determine whether to execute the cycle next time

int i=0;
while(i<100){
    
    
    i++;
    if(i%10==0){
    
    
        System.out.println();
        continue;
    }
    System.out.print(i+"\t");
}
    /*输出结果:
1	2	3	4	5	6	7	8	9	
11	12	13	14	15	16	17	18	19	
21	22	23	24	25	26	27	28	29	
31	32	33	34	35	36	37	38	39	
41	42	43	44	45	46	47	48	49	
51	52	53	54	55	56	57	58	59	
61	62	63	64	65	66	67	68	69	
71	72	73	74	75	76	77	78	79	
81	82	83	84	85	86	87	88	89	
91	92	93	94	95	96	97	98	99	
*/

goto keyword

  • There is no goto in java. However, in the two keywords of break and continue, we can still see some shadows of goto, the labeled break and continue.

  • "Label" refers to an identifier followed by a colon, for example: label:

  • Before the only label in Java is used in a loop statement, we hope to nest another loop in it. The break and continue keywords usually only interrupt the current loop, but if they are used with the label, they will be interrupted to the place where the label exists.

//打印101-150之间所有的质数
int count = 0;
oouter:for (int i=101;i<150;i++){
    
    
    for (int j =2;j<i/2;j++){
    
    
        if(i%j==0){
    
    
            continue oouter ;
        }
    }
    System.out.print(i+" ");
}
 /*输出结果:
101 103 107 109 113 127 131 137 139 149 
*/

Print triangle

//打印三角形   5行

for(int i=1;i<=5;i++){
    
    
    for (int j=5;j>=i;j--){
    
    
        System.out.print(" ");
    }
    for (int j=1;j<=i;j++){
    
    
        System.out.print("*");
    }
    for (int j=1;j<i;j++){
    
    
        System.out.print("*");
    }
    System.out.println("");
}
/*输出结果:
     *
    ***
   *****
  *******
 *********
*/

Debug usage

Insert picture description here

Insert picture description here

Insert picture description here

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44302662/article/details/114223341