当前位置:网站首页>Jump statements break and continue

Jump statements break and continue

2022-07-24 03:16:00 Start

break keyword

break It is mainly used in loop statements or switch In the sentence , Used to jump out of the whole statement block .

break Jump out of the innermost loop , And continue to execute the following statement of the loop .

grammar

break Is very simple to use , It's a statement in a loop structure

break;

Look at a case :

/*
		 * break:
		 * 	 Action scenario :switch Select structure and cycle structure 
		 * 	 effect :
		 * 		 stay switch Encountered in the selection structure break, End the whole switch Selection structure , perform switch Select behind structure             
                  Code for 
		 * 		 Encountered in the loop structure break, Then all subsequent operations of the whole cycle structure are ended , Execute the code after the loop structure 
		 * 	 Be careful :
		 * 		break General combination if Select statements to use together 
		 * 
		 * 
		 */
	public static void main(String[] args) {
// 1~10 Add integers between , The cumulative value is greater than 20 The current number of 
		int sum =0;
		for(int j=1;j<=10;j++){
			sum+=j;
			System.out.println("sum="+sum);
			System.out.println("j="+j);
			if(sum>20){
				
				break;
			}
		}

Running results

 

 

 break Use in double loop , In the inner loop break The end is the inner loop , If you want to end the outer loop , You need to use tags to end which loop the table name

public static void main(String[] args) {
				//a: b: Marking loops 
				a:for (int i = 1; i <= 5; i++) {
					System.out.print(i + "--");
					b:for (int j = 10; j <= 50; j += 10) {
						System.out.print(j + " ");
						if(j==30){
							break a;// Terminate the entire outer cycle 
						}
					}
					System.out.println();
				}

Running results :

 

 

continue keyword

continue It can be used in any cycle control structure . The function is to make the program jump to the next iteration of the loop immediately .

stay for In circulation ,continue Statement causes the program to jump to the update statement immediately .

stay while perhaps do…while In circulation , The program immediately jumps to the judgment statement of Boolean expression .

grammar

continue Is a simple statement in the loop body :

continue;

See the sample :

/*
		 * continue:
		 * 	 Action scenario :
		 * 		 It can only act in the circular structure 
		 * 	 effect :
		 * 		 End the present ( This time ) Cycle subsequent operations , Continue with the next cycle 
		 * 	 Be careful :
		 * 		continue General combination if Statement together 
		 * 
		 * 
		 */
	public static void main(String[] args) {
		//  Output 1-100 Even values between 
		for(int i =1;i<=100;i++){
//			 Yes even number allowed output 
			if(i%2==0){
				System.out.println("i="+i);
			}
//			 Otherwise, from the beginning 
			else{
				continue;
			}
		}

 

continue It can also be used in double circulation , In the inner loop continue The end is the inner loop , If you want to end the outer loop , You need to use tags to end which loop the table name

原网站

版权声明
本文为[Start]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207222304220218.html