aboutsummaryrefslogtreecommitdiff
path: root/files/ko/web/javascript/reference/statements/while/index.html
blob: bde7b43330dd6b6e48f602be86735ff2d024db38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
---
title: while
slug: Web/JavaScript/Reference/Statements/while
tags:
  - 반복문
  - 자바스크립트
translation_of: Web/JavaScript/Reference/Statements/while
browser-compat: javascript.statements.while
---
<div>{{jsSidebar("Statements")}}</div>

<p><strong>while문</strong>은 조건문이 참일 때 실행되는 반복문이다. 조건은 문장안이 실행되기 전에 참, 거짓을 판단한다.</p>

<h2 id="문법">문법</h2>

<pre class="syntaxbox">while (<em>condition</em>)
  <em>statement</em>
</pre>

<dl>
 <dt><code>조건</code></dt>
 <dd>반복이 시작되기 전에 조건문은 참,거짓을 판단받게 된다. 만약 조건문이 참이라면, while문 안의 문장들이 실행된다. 거짓이라면, 문장은 그냥 while 반복문 후로 넘어간다.</dd>
 <dt><code>문장</code></dt>
 <dd>조건문이 참일 때만 while문 속의 문장들이 실행된다. 반복문 속에 여러개의 문장을 사용하고 싶다면 중괄호 { } 를 통해 문장들을 하나로 묶어야 한다.</dd>
</dl>

<h2 id="예제">예제</h2>

<p>다음의 while문은 n이 3보다 작을 때까지 반복한다.</p>

<pre class="brush:js">var n = 0;
var x = 0;

while (n &lt; 3) {
  n++;
  x += n;
}</pre>

<p>반복을 살펴보면, n을 x에 계속 더하게 된다. 그러므로 x와 n 변수는 다음의 값을 갖는다.</p>

<ul>
 <li>첫번째 반복; n=1 과 x=1</li>
 <li>두번째 반복; n=2 과 x=3</li>
 <li>세번째 반복; n=3 과 x=6</li>
</ul>

<p>세번째 반복후, n&lt;3 이라는 초건은 더 이상 참이아니가 되므로 반복은 종료된다</p>

<h2 id="specifications">명세</h2>

<p>{{Specifications}}</p>

<h2 id="browser_compatibility">브라우저 호환성</h2>

<p>{{Compat}}</p>

<h2 id="See_also">See also</h2>

<ul>
 <li><a href="/en-US/docs/Web/JavaScript/Reference/Statements/do...while"><code>do...while</code></a></li>
 <li>{{jsxref("Statements/for", "for")}}</li>
</ul>