1)Switch
사용빈도는 적지만 조건이 많다면 switch문이 로직을 보다 명료하게 보여줄 수 있다.
switch 문을 사용할 때 한가지 주의 할 것은 switch의 조건으로는 몇가지 제한된 데이터 타입만을 사용할 수 있다는 것이다.
byte, short, char, int, enum, String, Character, Byte, Short, Integer
package org.opentutorials.javatutorials.condition;
public class SwitchDemo {
public static void main(String[] args) {
System.out.println("switch(1)");
switch(1) {
case 1:
System.out.println("one"); // one 출력
case 2:
System.out.println("two"); // two 출력
case 3:
System.out.println("three"); // three 출력
}
switch(2) {
case 1:
System.out.println("one");
case 2:
System.out.println("two"); // two 출력
case 3:
System.out.println("three"); // three 출력
}
switch(3) {
case 1:
System.out.println("one");
case 2:
System.out.println("two");
case 3:
System.out.println("three"); // three 출력
}
switch(1) {
case 1:
System.out.println("one"); // one 출력
break;
case 2:
System.out.println("two");
case 3:
System.out.println("three");
}
switch(1) {
case 1:
System.out.println("one"); // one 출력
break;
case 2:
System.out.println("two");
break;
case 3:
System.out.println("three");
break;
default:
System.out.println("default");
}
}
}
출처 - 생활코딩
'자바' 카테고리의 다른 글
| 11) 반복문 (0) | 2021.10.18 |
|---|---|
| 10) 논리 연산자 (0) | 2021.10.18 |
| 8) 비교와 Boolean (0) | 2021.10.18 |
| 7) 연산자 (0) | 2021.10.18 |
| 6) 형변환 (0) | 2021.10.18 |