> For the complete documentation index, see [llms.txt](https://nbcamp.gitbook.io/java-handbook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://nbcamp.gitbook.io/java-handbook/part-03./switch.md).

# SWITCH

## switch

> ### switch (조건식) {&#x20;
>
> ### &#x20;         case 값1:&#x20;
>
> ### &#x20;                     조건식의 결과가 <mark style="color:blue;">값1과 같을 경우</mark> 수행할 문장;&#x20;
>
> ### &#x20;                     break;
>
> ### &#x20;         case 값2:&#x20;
>
> ### &#x20;                     조건식의 결과가 <mark style="color:blue;">값2와 같을 경우</mark> 수행할 문장;&#x20;
>
> ### &#x20;                     break;&#x20;
>
> ### &#x20;           .... &#x20;
>
> ### &#x20;          default: &#x20;
>
> ### &#x20;                      조건식의 결과와 <mark style="color:blue;">일치하는 case 문이 없을 때</mark> 수행할 문장;&#x20;
>
> ### }&#x20;

* 처리해야 하는 <mark style="color:blue;">경우의 수가 많을 때</mark> 유용한 조건문입니다.
* <mark style="color:blue;">break;</mark> 를 작성해 주지 않으면 <mark style="color:blue;">switch 문 끝</mark>까지 실행됩니다.
* <mark style="color:blue;">default 문은 생략</mark> 가능합니다.
* if 조건문과 비교해보면 <mark style="color:blue;">if</mark> 는 조건식 결과에 <mark style="color:blue;">true/false 만 가능</mark>하고 <mark style="color:blue;">switch</mark> 는 <mark style="color:blue;">정수나 문자열 만 가능</mark>합니다.
* 실행 흐름 확인하기
  1. 조건식을 계산한다.
  2. 조건식의 결과와 일치하는 case 문으로 이동한다.
  3. 해당 case 문의 문장들을 수행한다.
  4. break; 를 만나거나 switch 문이 끝나면 switch 문 전체를 빠져나간다.

### switch 문의 제약조건

1. switch 문의 <mark style="color:blue;">조건식 결과</mark>는 <mark style="color:blue;">정수 또는 문자열</mark> 이어야 합니다.
2. case 문의 값은 <mark style="color:blue;">정수 상수(문자 포함), 문자열</mark> 만 가능하며, <mark style="color:blue;">중복되지 않아야</mark> 한다.

> int num, result;
>
> final int ONE = 1;
>
> switch (result) {
>
> &#x20;        case '1':             // OK. 문자 리터럴(정수 49와 동일)
>
> &#x20;        case ONE:         // OK. 정수 상수
>
> &#x20;        case "YES"        // OK. 문자열 리터럴
>
> &#x20;        case num:         // Error. 변수는 불가능&#x20;
>
> &#x20;        case 1.0:            // Error. 실수도 불가능
>
> }

* 아래 예제코드로 학습해 보겠습니다.

{% code lineNumbers="true" %}

```java
class Control2_1 {
    public static void main(String[] args) {
        int month = 8;
        String monthString = "";
        switch (month) {
            case 1:  monthString = "January";
                     break;
            case 2:  monthString = "February";
                     break;
            case 3:  monthString = "March";
                     break;
            case 4:  monthString = "April";
                     break;
            case 5:  monthString = "May";
                     break;
            case 6:  monthString = "June";
                     break;
            case 7:  monthString = "July";
                     break;
            case 8:  monthString = "August";
                     break;
            case 9:  monthString = "September";
                     break;
            case 10: monthString = "October";
                     break;
            case 11: monthString = "November";
                     break;
            case 12: monthString = "December";
                     break;
            case 0: case 13:
                     System.out.println("이런식으로 case 문을 사용할 수 있습니다.");
                     break;
            case 15:
            default: monthString = "Invalid month";
        }
        System.out.println(monthString);
    }
}
```

{% endcode %}

{% hint style="info" %}
Ref. Java의 정석 기초편 Chapter4(9, 10. 11)

Ref. [switch](https://www.youtube.com/watch?v=8ZfCd08nnag\&list=PLW2UjW795-f6xWA2_MUhEVgPauhGl3xIp\&index=34)
{% endhint %}
