Get started with java-3.2 return break continue

Source: Internet
Author: User

Get started with java-3.2 return break continue

In this chapter, return break continue is directly the same and different.

1. Same

Are all out of the loop.

 

package com.ray.ch03;public class Test {public static void main(String[] args) {System.out.println(-------with break-------);for (int i = 0; i < 10; i++) {if (i == 5) {break;}System.out.println(i);}System.out.println(-------with continue-------);for (int i = 0; i < 10; i++) {if (i == 5) {continue;}System.out.println(i);}System.out.println(-------with return-------);for (int i = 0; i < 10; i++) {if (i == 5) {return;}System.out.println(i);}System.out.println(-------if end?-------);}}

Output:

 

------- With break -------
0
1
2
3
4
------- With continue -------
0
1
2
3
4
6
7
8
9
------- With return -------
0
1
2
3
4

From the output, we can see that all three are out of the loop, but here we will introduce the following differences.

 

2. Differences

Continue to observe the above results

Break: jumps out when I = 5 and does not output cyclically.

Continue: jumps out when I = 5, but continues to execute other Loops

Return: directly jump out of the method when I = 5, no code is executed, and the output of the last sentence is not executed.

 

Summary: This chapter briefly introduces the direct similarities and differences between return break continue.

 

 

 

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.