Skip to content

Commit 9c68a27

Browse files
Add example break - continue
1 parent 99049c4 commit 9c68a27

File tree

4 files changed

+164
-46
lines changed

4 files changed

+164
-46
lines changed

.idea/vcs.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

Lines changed: 100 additions & 46 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.tiennguyen.chapter_3.break_continue;
2+
3+
import java.util.Date;
4+
5+
public class MainBreakContinue {
6+
7+
public static void main(String[] args) {
8+
MainBreakContinue main = new MainBreakContinue();
9+
main.breakInSwitchCase();
10+
main.breakInWhile();
11+
main.continueInFor();
12+
}
13+
14+
/**
15+
* break command line is used in switch ... case ...
16+
* Case 1: Show `This is a car`
17+
* Case 2: Show `This is bicycle`
18+
* Default: Show `I don't known`
19+
*/
20+
public void breakInSwitchCase() {
21+
int carCode = 1;
22+
switch (carCode) {
23+
case 1:
24+
System.out.println("This is a car.");
25+
break;
26+
case 2:
27+
System.out.println("This is a bicycle.");
28+
break;
29+
default:
30+
System.out.println("I don't known");
31+
}
32+
}
33+
34+
/**
35+
* print number day <= 15. If day > 15 system will `break` it.
36+
*/
37+
public void breakInWhile() {
38+
int day = 1;
39+
while (day <= 29) {
40+
day++;
41+
42+
if (day > 15) {
43+
break;
44+
}
45+
46+
System.out.println(String.format("Today is %s", day));
47+
}
48+
}
49+
50+
/**
51+
* continue in for command line.
52+
* loop from 0 to 9. if it is even number, print it in console.
53+
*/
54+
public void continueInFor() {
55+
int numberOfLoop = 10;
56+
for (int i = 0; i < numberOfLoop; i++) {
57+
if (i % 2 != 0) {
58+
continue;
59+
}
60+
System.out.println("This is even number : " + i);
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)