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
'JavaScript > Study' 카테고리의 다른 글
JS 반복문 (for 문) (0) | 2021.07.16 |
---|---|
JS 반복문 (do / while 문) (0) | 2021.07.16 |
JS 삼항 연산자 (표현식 ? 반환식1 : 반환값2) (0) | 2021.07.15 |
JS 논리 연산자 (&&, ||, !) (0) | 2021.07.15 |
JS 비교 연산자 (동등 연산자, 일치 연산자, ==, ===, !=, !==, >, >=, <, <=) (0) | 2021.07.15 |