웹 프로그래밍 기초/조건문

if ~ else문

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

01. 사용 목적

조건이 거짓일 때 실행해야 하는 문장을 else 다음에 기술한다

 

02. 사용법

if (조건식) {

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

}

else {

  // 조건식이 거짓일 때의 표현식

}

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var time = 21;

    if (time > 22) {
      document.write("Good Night..." + "<br>");
    }
    else {
      document.write("Good Afternoon" + "<br>");
    }
    document.write("TV를 본당")
  </script>

</body>

</html>

'웹 프로그래밍 기초 > 조건문' 카테고리의 다른 글

연습문제 1  (0) 2025.04.08
switch문  (0) 2025.04.03
중첩된 if문  (0) 2025.04.03
if else - if 문  (0) 2025.04.03
if문  (0) 2025.04.03