별초롱언니 2025. 4. 3. 10:54

01. 사용목적

결과가 2개인 경우가 아닌 조건을 여러개로 나누어 결과를 여러개로 보고싶을때 사용한다. 

 

02. 사용법

if (조건식1) {

  // 조건식1이 참일 때의 표현식

} else if (조건식2) {

  // 조건식2가 참일 때의 표현식

} else {

  //조건식1과 2가 모두 거짓일 때의 표현식

}

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>if else-if문</title>
</head>
<body>
  <script>
    var score = Number(prompt("학생 점수를 입력하세요","0~100 사이의 정수"));

    if (score > 90) {
      alert("A학점");
    } else if (score > 80) {
      alert("B학점");
    } else if (score > 70) {
      alert("C학점");
    } else if (score > 60) {
      alert("D학점");
    } else {
      alert("E학점");
    }
  </script>
</body>
</html>