Skip to content

Commit d754737

Browse files
committed
chap04 제어 흐름 이해하기 완료
1 parent 0c9dbb8 commit d754737

13 files changed

Lines changed: 218 additions & 0 deletions

src/chap04/BreakExample.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package chap04;
2+
3+
public class BreakExample {
4+
public static void main(String[] args) {
5+
int sum = 0;
6+
int num = 1;
7+
8+
while (true) {
9+
sum += num;
10+
if (sum > 100) {
11+
break;
12+
}
13+
num++;
14+
}
15+
System.out.println(sum);
16+
System.out.println(num);
17+
}
18+
}

src/chap04/ContinueExample.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package chap04;
2+
3+
public class ContinueExample {
4+
public static void main(String[] args) {
5+
int total = 0;
6+
7+
for (int num = 1; num <= 100; num++) {
8+
if (num % 2 == 0) {
9+
continue;
10+
}
11+
total += num;
12+
}
13+
System.out.println(total);
14+
}
15+
}

src/chap04/DoWhileExample.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package chap04;
2+
3+
public class DoWhileExample {
4+
public static void main(String[] args) {
5+
int num = 1;
6+
int sum = 0;
7+
8+
do {
9+
sum += num;
10+
num++;
11+
} while (num <= 10);
12+
13+
System.out.println("1부터 10까지의 합은 " + sum + "입니다.");
14+
}
15+
}

src/chap04/ForExample.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package chap04;
2+
3+
public class ForExample {
4+
public static void main(String[] args) {
5+
int num, sum;
6+
for (num = 1, sum = 0; num <= 10; num++) {
7+
sum += num;
8+
}
9+
System.out.println("1부터 10까지의 합은 " + sum + "입니다.");
10+
}
11+
}

src/chap04/ForExample2.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package chap04;
2+
3+
public class ForExample2 {
4+
public static void main(String[] args) {
5+
int dan, times;
6+
7+
for (dan = 2; dan <= 9; dan++) {
8+
for (times = 1; times <= 9; times++) {
9+
System.out.println(dan + "X" + times + "=" + dan * times);
10+
}
11+
System.out.println();
12+
}
13+
}
14+
}

src/chap04/ForExample3.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package chap04;
2+
3+
public class ForExample3 {
4+
public static void main(String[] args) {
5+
for (int i = 0; i < 10; i++) {
6+
System.out.println(i + "hello world!");
7+
}
8+
9+
System.out.println();
10+
11+
for (int i = 1; i <= 10; i++) {
12+
System.out.println(i + "hello world!");
13+
}
14+
}
15+
}

src/chap04/ForExample4.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package chap04;
2+
3+
public class ForExample4 {
4+
public static void main(String[] args) {
5+
int dan, times;
6+
7+
for (dan = 2; dan <= 9; dan++) {
8+
for (times = 1; times <= 9; times++) {
9+
if (dan % 2 == 1) {
10+
break;
11+
}
12+
System.out.println(dan + "X" + times + "=" + dan * times);
13+
}
14+
System.out.println();
15+
}
16+
}
17+
}

src/chap04/IfExample1.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package chap04;
2+
3+
public class IfExample1 {
4+
public static void main(String[] args) {
5+
int age = 2;
6+
7+
if (age >= 8) System.out.println("학교에 다닙니다.");
8+
else {
9+
System.out.println("학교에 다니지 않습니다.");
10+
}
11+
}
12+
}

src/chap04/IfExample2.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package chap04;
2+
3+
public class IfExample2 {
4+
public static void main(String[] args) {
5+
int age = 9;
6+
int charge;
7+
8+
if (age < 8) {
9+
charge = 1000;
10+
System.out.println("취학 전 아동입니다.");
11+
}
12+
else if (age < 14) {
13+
charge = 2000;
14+
System.out.println("초등학생입니다.");
15+
}
16+
else if (age < 20) {
17+
charge = 2500;
18+
System.out.println("중, 고등학생입니다.");
19+
}
20+
else {
21+
charge = 3000;
22+
System.out.println("일반인입니다.");
23+
}
24+
System.out.println("입장료는 " + charge + "원입니다.");
25+
}
26+
}

src/chap04/SwitchCase.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package chap04;
2+
3+
public class SwitchCase {
4+
public static void main(String[] args) {
5+
int rank = 1;
6+
char medalColor;
7+
8+
switch (rank) {
9+
case 1: medalColor = 'G';
10+
break;
11+
case 2: medalColor = 'S';
12+
break;
13+
case 3: medalColor = 'B';
14+
break;
15+
default: medalColor = 'A';
16+
}
17+
System.out.println(rank + "등의 메달 색은 " + medalColor + "입니다.");
18+
}
19+
}

0 commit comments

Comments
 (0)