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.