# switch문

### switch문

> let 변수 = 초기값;\
> switch (조건을 체크할 변수) {\
> &#x20;   case 값1: 코드1;\
> &#x20;       //조건을 체크할 변수가 값1을 가지면 실행\
> &#x20;   break;\
> &#x20;   case 값2: 코드2;\
> &#x20;       //조건을 체크할 변수가 값2을 가지면 실행\
> &#x20;   break;\
> &#x20;   case 값3: 코드3;\
> &#x20;       //조건을 체크할 변수가 값3을 가지면 실행\
> &#x20;   break;\
> &#x20;   case 값4: 코드4;\
> break;\
> default;\
> &#x20;   //해당되는 값을 가지고 있지 않을 경우 실행\
> }

```javascript
let first = prompt("제일 먼저 잡고 싶은 물건은 무엇인가요? 1.과일 2. 떡 3.수정구슬 4.현찰 5.반지")

switch(first){
    case "과일" :
        document.write("생각이 많고 진지해 보이는 연상타입니다.");
    break;
    case "떡" :
        document.write("무드에 약해 화술과 매너가 좋은 타입입니다.");
    break;
    case "수정구슬" :
        document.write("정열적이고 열중하는 타입입니다..");
    break;
    case "현찰" :
        document.write("부드럽고 따듯한 타입입니다.");
    break;
    case "반지" : 
        document.write("자기를 보호해 줄 수 있는 이성 타입입니다.");
    break;
    default:
        document.write("잘못 선택했습니다.");
    break;
}
```

#### 다중 if문→swifch

```javascript
let score = prompt("당신의 자바스크립트 점수는?");
let grade;

if(score >= 90){
    //document.write("당신의 학점은 A입니다.");
    grade = "A";
} else if(score >= 80 && score < 90){
    //document.write("당신의 학점은 B입니다.");
    grade = "B";
} else if(score >= 70 && score < 80){
    //document.write("당신의 학점은 C입니다.");
    grade = "C";
} else if(score >= 60 && score < 70){
    /document.write("당신의 학점은 D입니다.");
    grade = "D";
} else if(score > 60){
    //document.write("당신의 학점은 F입니다.");
    grade = "F";
    
//switch문은 조건문 쓸 수 없음
switch(grade){
    case "A" :  document.write("당신의 학점은 A입니다.");
    break;
    case "B" :  document.write("당신의 학점은 B입니다.");
    break;
    case "C" :  document.write("당신의 학점은 C입니다.");
    break;
    case "D" :  document.write("당신의 학점은 D입니다.");
    break;
    case "F" :  document.write("당신의 학점은 F입니다.");
    break;
}
```

#### swifch→다중 if문

```javascript
let site = prompt("네이버, 다음, 네이트, 구글 중 즐겨찾는 검색사이트는 무엇인가요?")
let url;

switch(site){
    case "구글" : url = "https://www.google.co.kr/";
    break;
    case "네이버" : url = "https://www.naver.com/";
    break;
    case "다음" : url = "https://www.daum.net/";
    break;
    case "네이트" : url = "https://www.nate.com/";
    break;
    default : document.write("그런 사이트는 없습니다.");
    break;
    //console.log(site);
    //console.log(url);
}
if(url){
    location.href = url;
}

//다중if문으로 바꾸기
if(site=="구글"){
    url = "https://www.google.co.kr/";

} else if(site=="네이버"){
    url = "https://www.naver.com/";

} else if(site=="다음"){
    url = "https://www.daum.net/";

} else if(site=="네이트"){
    url = "https://www.nate.com/";

} else {
    document.write("그런 사이트는 없습니다.");
}
```


---

# 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://ayou09110.gitbook.io/javascript-jquery/undefined-1/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.
