# switch

처리해야하는 경우의 수가 많을 때 유용한 조건문이다.

```java
switch(조건식) {
    case 값1 :
        ...
        break;
    case 값2 :
        ...
        break;
    default :
        //조건식의 결과와 일치하는 case문이 없을 때 수행될 문장들.
        //break문 불필요
}
```

**switch문의 제약조건**

```java
int num, result;
final int ONE = 1;//상수!
...
switch(result) {
    case '1'://문자 리터럴=>정수 49와 동일
    case ONE://정수 상수
    case "YES"://문자열 리터럴. JDK 1.7부터 허용
    case num://error. 변수는 불가
    case 1.0://error. 실수도 불가
}
```

case문 예시: break문을 깜빡하기 쉬운데, default문에서는 생략이 가능하지만 break문을 case마다 해주지 않으면 다음 break문을 만날 때까지 무분별하게 코드를 실행한다!

```java
import java.util.Scanner;

class season {
	public static void main(String[] args) {
		System.out.print("현재 월을 입력하세요.>");
		
		Scanner scanner = new Scanner(System.in);
		int month = scanner.nextInt();
		
		switch(month) {
		case 3: 
		case 4:
		case 5:
			System.out.println("현재의 계절은 봄입니다.");
			break;
		case 6: case 7: case 8:
			System.out.println("현재의 계절은 여름입니다.");
			break;
		case 9: case 10: case 11:
			System.out.println("현재의 계절은 가을입니다.");
			break;
		default:
			//case 12: case 1: case 2:
			System.out.println("현재의 계절의 겨울입니다.");
			//break문 불포함가능. 
		}
	}
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://heunnajo.gitbook.io/java/ch4/switch.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
