728x90
반응형

 

while
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>while문</title>
    <script>
      var i = 1;
      while (i <= 10) {
        document.write("안녕하세요" + i, "<br>");
        i++; //i = i+1;
      }
      document.write("==== The End ==== <br>");

      var i = 1; //재할당o, 재선언x
      while (i <= 30) {
        if (i % 2 == 0 && i % 6 == 0) {
          document.write(i, "<br>");
        }
        i++;
      }
      document.write("==== The End ==== <br>");
    </script>
  </head>
  <body></body>
</html>​

 

while
if ~ else
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>while문</title>
    <style>
      * {margin: 0;}
      .blue {color: #00f;}
      .red {color: #f00;}
    </style>
  </head>
  <body>
    <script>
      var i = 20;
      while (i >= 10) {
        if (i % 2 == 0) {
          document.write('<span class="blue">' + i + "</span> ");
        } else {
          document.write('<span class="red">' + i + "</span> ");
        }
        i--;
      }
    </script>
  </body>
</html>​

do~while
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>반복문</title>
  </head>
  <body>
    <h1>do/while문</h1>
    <script>
      var i = 10;
      do {
        document.write("hello!!");
      } while (i < 3);

      var i = 1,
        j = 1;
      while (i < 3) {
        document.write("i : " + i++ + "<br>"); //1(2)->2(3)
      }

      do {
        document.write("j : " + j++ + "<br>"); //1(2)
      } while (j > 3);
    </script>
  </body>
</html>
728x90
반응형

'[JS]' 카테고리의 다른 글

[JS] continue  (0) 2024.02.27
[JS] for  (0) 2024.02.27
[JS] 조건문  (0) 2024.02.27
[JS] Date  (0) 2024.02.27
operator  (0) 2024.02.27

+ Recent posts