Java technology _java Thousands of questions (0020) _break and contine how to use each

Source: Internet
Author: User

Click to enter _ more _java thousand ask


how break and Contine are used separately

Break,contine are used in the loop body of the statement, there is the role of termination, the specific differences to see the following explanation. The Loop statement looks here:

1. How the Break keyword is used:Break is used to stop the entire loop . Must be in the loop body or in the switch statement (switch syntax here: How switch is used). It will stop the execution of this layer loop (the multilayer loop will only stop the break layer, go out of the parent loop), and begin executing the subsequent code.
Grammar
The break syntax is a separate statement in any loop:
Break

Example:

public class Test {public static void main (String args[]) {int [] numbers = {Ten, A, a,, 50};for (int x:numbers) {if (x = = 30) {break;} SYSTEM.OUT.PRINTLN (x);}}}
this will produce the following results:
10
-

2. How the Continue keyword is used:Continue is used to stop this code in the loop, but the loop continues . It causes the loop to jump immediately to the next iteration, continuing the loop.
In the For loop, continue executes the UPDATE statement immediately. In a while loop or Do/while loop, the flag value is immediately judged. Java looping syntax see here: How to Loop through Java
Grammar
The continue syntax is a separate statement in any loop:
Continue

Example:

public class Test {public static void main (String args[]) {int [] numbers = {Ten, A, a,, 50};for (int x:numbers) {if (x = = 30) {continue;} SYSTEM.OUT.PRINTLN (x);}}}

This will produce the following results:

10
20
40
-

Click to enter Ooppookid's blog


Java technology _java Thousands of questions (0020) _break and contine how to use each

Related Article

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.