본문 바로가기
Web/JavaScript

[JavaScript] 제어문 - 1️⃣ 조건문 (if문, swith문)

by coding-choonsik 2023. 4. 7.
728x90
반응형
SMALL

1. if문

  • 조건의 범위가 있을 때 사용

 

1-1. if문

if(조건식){
        조건식의 결과가 true일 때 실행할 문장
        ...
    }

 


1-2.  if~else문

 if(조건식){
        조건식의 결과가 true일 때 실행할 문장
        ...
    }else{
        조건식의 결과가 false일 때 실행할 문장
        ...
    }

1-3.  if~else if~else문

if(조건식1){
    조건식1의 결과가 true일 때 실행할 문장
    ...
}else if(조건식2){
    조건식2의 결과가 true일 때 실행할 문장
}else if(조건식3){
    조건식3의 결과가 true일 때 실행할 문장

...
}else{
    모든 조건식의 결과가 false일 때 실행할 문장
}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>if문</title>
</head>
<body>
    <h2>if문</h2>
    <script>
        const age = Number(prompt('나이를 입력하세요'))

        if(age > 19){
            console.log('성인입니다')
        }else if(age > 14){
            console.log('청소년입니다')
        }else if(age > 6){
            console.log('어린이입니다')
        }else{
            console.log('유아입니다')
        }
    </script>
</body>
</html>

▲ prompt 입력창

 

▲ 실행 결과


2. switch문

  • 일치하는 값이 있을 때 사용
switch(변수){
    case 값1:
        변수와 값1이 같을 경우 실행할 문장
        ...
        break
    case 값2:
        변수와 값2가 같을 경우 실행할 문장
        ...
        break
    case 값3:
        변수와 값3이 같을 경우 실행할 문장
        ...
        break  // break가 없으면 무조건 default문 실행
    ...

    default:
        변수와 모든 값이 다를 경우 실행할 문장
}

 


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>swith문</title>
</head>
<body>
    <h2>swith문</h2>
    <script>
        let input = prompt('이동, 청소년, 성인 중 하나를 고르세요')

        // 아동, 청소년, 성인 중 1
        switch(input){
            case '아동':
                input += ': 입장료 무료'  // '아동: 입장료 무료'
                break
            case '청소년':
                input += ': 입장료 40,000원' // '청소년: 입장료 40,000원'
                break
            case '성인':
                input += ': 입장료 60,000원'    // '성인: 입장료 60,000원'
                break  

            default:
                alert('입력값을 확인하세요!')
                input='입력값 확인!'
        }
        console.log(input)
    </script>
    
</body>
</html>

▲ prompt 입력창
▲ 실행 결과

 


2-1. 조건이 여러개인 경우

 

✅ 달(month)을 입력받아 해당 달의 마지막 일이 몇일인지 출력하는 문서를 작성해보자.(단, 입력은 prompt 를 사용하고 조건문은 switch문을 사용)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>month</title>
</head>
<body>
    <h2>month</h2>
    <script>
        let month = Number(prompt('달을 입력하세요.'))
        switch(month){
            case 1:  // case를 일렬로 작성해도됨!
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                console.log(`${month}월의 마지막 일자는 31일 입니다.`)
                break
            case 2:
                console.log(`${month}월의 마지막 일자는 28일 입니다.`)   
                break
            case 4:
            case 6:
            case 9: 
            case 11:
                console.log(`${month}월의 마지막 일자는 30일 입니다.`)
                break
            default:
                alert('입력값을 확인하세요!')
                month='입력값 확인!'
        }
        
    </script>
</body>
</html>

▲ prompt 입력창
▲ 실행 결과

 

 

728x90
반응형
LIST