← Back to feed
Renewal·서른의 생활코딩

Sample Source 2) JavaScript Basics (While Loop)

NS
normalstory
cover image



while문 1.


A while loop is essentially a for loop spelled out line by line. (purely personal opinion..)

Why?

If you read the source carefully,

the conditions packed inside () of a for loop

are written out one line at a time.


First, an example that lists numbers.



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"


"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ko" lang="ko">




<head>


<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" />


<title> while _1 </title>


</head>




<body>




<script type="text/javascript">


//<!CDATA[




// while the condition is true, repeat the statement.




var i=1;




while(i<=10){


document.write("loop" + i + "<br />");


i++;


}




//]]]]>


</script>




</body>


</html>








while문 2.


This time, let us list numbers with gaps.

For that, we need to combine it with an if statement.



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ko" lang="ko">




<head>


<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" />


<title> while _3 </title>


</head>




<body>




<script type="text/javascript">


//<!CDATA[




// from 1~30, print only multiples of 3




var i=1




while(i<=30){


if(i%3==0){


document.write(i + "<br />");


}


i++;


}






//]]]]>


</script>




</body>


</html>while문 3.조건을 한번더 꼬아보자..


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"


"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ko" lang="ko">




<head>


<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" />


<title> while _3 </title>


</head>




<body>




<script type="text/javascript">


//<!CDATA[




/* from 1~30, print only multiples of 3 and 5




var i=1




while(i<=30){


if(i%3==0){


document.write("multiple of 3 " + i + "<br />");


}else if(i%5==0){


document.write("multiple of 5 " +i + "<br />");


}


i++;


}




//






var i=1;




while(i<=30){


if(i%2==0||i%5==0){


document.write(i + "<br />");


}


i++;


}




*/




var i=1




var a3= "";


var a5= "";




while(i<=30){


if(i%3==0){


a3 += i+ ""+ "<br />";


}


if(i%5==0){


a5 += i+ ""+ "<br />";


}


i++


}


document.write(a3 + "<br />");


document.write(a5);






//]]]]>


</script>




</body>


</html>

+ Oh and !do while문.for 문과 while문은 조건에만 만족해야 실행할 수 있다.하지만 만약,예외처리 하고싶은 경우가 생긴다면???사람사는 환경에서 그렇듯 프로그렘에서도 그런일들이 종종있다보다...유두리 있는 프로그렘의 세계...?



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ko" lang="ko">




<head>


<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" />


<title> do-while _1 </title>


</head>




<body>




<script type="text/javascript">


//<!CDATA[


/*


*/


var num=4;


do{


document.write(num+"<br />");


}while(num<3);




//]]]]>


</script>




</body>


</html>

This English version was translated by Claude.

친절한 찰쓰씨
Written by
친절한 찰쓰씨

Pleasant Charles — UI/UX researcher at AIT. Keeping notes on design, planning, and slow days here since 2010.