javaScript/J Query & 스크립트

while문에서 continue문과 break문

mooyou 2019. 3. 20. 07:55
728x90
300x250
SMALL

continue는 while에서도 for 문과 똑같이 작동 된다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
    <script type="text/javascript">
 
        var i = 1;
        while (i <= 10) {
            i++;
            continue;
 
            document.write(i + "<br>");
        }
 
        document.write("최종 i=" + i + "<br>");
 
 
    </script>


실행결과:

최종 i=11




break문은 실행구문 중 break를 만나면 while문은 그대로 정지되며 while문의 루프를 빠져나온다 즉 while문을 강제로 빠져나오고 싶을때 사용하는 명령어


1
2
3
4
5
6
7
8
9
10
11
12
    <script type="text/javascript">
 
        var i = 1;
        while (i <= 10) {
            break;
            i++;
            document.write(i + "<br>");
        }
 
        document.write("최종 i=" + i + "<br>");
 
    </script>


실행결과

최종 i=1


반복하지 않고 바로 빠져나온다.

728x90
반응형
LIST