Java technology _ Java 0020 question () _ How to Use break and contine respectively

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.