티스토리 뷰

Javascript

[Javascript] while문

while문

반복되는 부분을 실행할 때 사용하는 제어문입니다.

while문

var 변수 = 초기값
while (조건식){
    실행문;
    증감식;
}

Sample : while문
<!DOCTYPE html>
<html lang="ko">
    <head>
    	<meta charset="utf-8">
        <title>자바스크립트(JavaScript)</title>
        <script type="text/javascript">
			var count = 0;	
			while(true){
				count++;	
				document.write("" + count + "번째 실행");
				document.write("<br />");
				
				if(count > 9){	// 조건
					break;
				}
			}
        </script>
    </head>
    
    <body>
	...
    </body>
</html>   
Sample : while문
<!DOCTYPE html>
<html lang="ko">
    <head>
    	<meta charset="utf-8">
        <title>자바스크립트(JavaScript)</title>
        <script type="text/javascript">
			var i = 100;
			while(i>0){	
				
				if(i%2==0){
					document.write("<span style='color:blue;'>"+i+"</span>","<br>");
				}else{
					document.write("<span style='color:red;'>"+i+"</span>","<br>");
				}
				
				i--;
			}	        
		</script>
    </head>
    
    <body>
	...
    </body>
</html>  
TAG
댓글
댓글쓰기 폼