생활코딩 JS 반복문

김정현·2022년 4월 20일
0
  • 반복문(loop, iterate)
  • while
while(조건){
반복해서 실행할 코드
} 입력하세요
<!DOCTYPE html>
<html>
    <head>
        <title></title>
    </head>
    <body>
        <script type="text/javascript">
        var i=0;
        while(i<10){
            document.write("coding everybody <br/>")
            i=i+1;
            }
        </script>
    </body>
</html>
<br/> - 줄바꿈
  • for
while(조건){
반복해서 실행할 코드
} 입력하세요
<!DOCTYPE html>
<html>
    <head>
        <title></title>
    </head>
    <body>
        <script type="text/javascript">
        for(var i =0;i<10;i++){
            document.write("coding everybody <br/>")
            }
        </script>
    </body>
</html>
  • 반복문의 제어
  • break - 반복문 종료
0~4까지 실행
 for(var i =0;i<10;i++){
 if(i===5){
 break;
            document.write("coding everybody <br/>")
  • continue - 그순간에만 종료 후 반복문 계속 진행
5에서 종료 후 9까지 실행
for(var i =0;i<10;i++){
 if(i===5){
 continue;
            document.write("coding everybody <br/>")
  • 반복문의 중첩
<!DOCTYPE html>
<html>
    <head>
        <title></title>
    </head>
    <body>
        <script type="text/javascript">
        for(var i =0;i<10;i++){
        for(var j=0;j<10;j++){
            document.write("coding everybody'+i+j+'<br/>")
            }
        </script>
    </body>
</html>

0개의 댓글