Java technology _ Java 0020 question () _ How to Use break and contine respectively
How to Use break and contine respectively
Break and contine are all statements used in the loop body and all have the function of terminating execution. For details about the differences, see the following. Here are the loop statements:
1. How to use the break Keyword: break is used to stop the entire loop. It must be in the loop body or switch statement (switch syntax here: How to Use switch ). It will stop the execution of this layer loop (the multi-layer loop will only stop the break layer, and then continue the parent level loop), and start to execute the subsequent code.
Syntax
The break syntax is a separate statement in any loop:
break;
Example:
public class Test {public static void main(String args[]) {int [] numbers = {10, 20, 30, 40, 50};for(int x : numbers ) {if( x == 30 ) {break;}System.out.println(x);}}}
This produces the following results:
10
202. How to Use the continue Keyword: continue is used to stop the code in the loop, but the loop continues. It immediately redirects the loop to the next iteration and continues the loop.
In the for loop, continue immediately executes the update statement. In a while loop or do/while loop, the flag value is determined immediately. Java loop Syntax: how to execute it cyclically in java
Syntax
The continue syntax is a separate statement in any loop:
continue;
Example:
public class Test {public static void main(String args[]) {int [] numbers = {10, 20, 30, 40, 50};for(int x : numbers ) {if( x == 30 ) {continue;}System.out.println(x);}}}
This produces the following results:
10
20
40
50