JS 반복문 (while 문)

2021. 7. 16. 01:06
728x90

 

- while 문은 특정 조건을 만족할 때까지 계속해서 주어진 실행문을 반복 실행

  while (표현식){

     표현식의 결과가 참이 될 동안 반복적으로 실행하고자 하는 실행문;

     

 

 

 

 

 

<!DOCTYPE html>
<html>
<head>
  <meta charset='utf-8'>
  <meta http-equiv='X-UA-Compatible' content='IE=edge'>
  <title>반복문 - while</title>
  <meta name='viewport' content='width=device-width, initial-scale=1'>
  <script>
      var i = 0;
      while(i < 5){
        document.write("Hello World","<br>");
        i++;
      }
  </script>
</head>
<body>
</body>
</html>
 
cs

 

 

 

< 숫자 1 ~ 10 합을 출력 >

<!DOCTYPE html>
<html>
<head>
  <meta charset='utf-8'>
  <meta http-equiv='X-UA-Compatible' content='IE=edge'>
  <title>반복문 while</title>
  <meta name='viewport' content='width=device-width, initial-scale=1'>
  <script>
     var i = 1;
     var sum = 0;
     while(i <= 10){
      sum += i;
      i++;
     }
     document.write(`1~10까지 합 : ${sum}<br>`);
  </script>
</head>
<body>
</body>
</html>
 
cs

 

728x90

BELATED ARTICLES

more