210328 JavaScript if문 연습

ITisIT210·2021년 3월 29일

JavaScript

목록 보기
11/18
post-thumbnail
<!DOCTYPE html>
<html lang="en">
    <head>
            <meta charset="UTF-8">
            <title>Document</title>
    </head>
    <boby>
        <script>
            //반복문 (for)
            /*
                for(초기값; 조건식; 증감식;) {
                    초기값이 조건식에 대해 참인 경우 반복 될 실행 구문
                }
            */

            // document.write("1. 홍길동 <hr>");
            // document.write("1. 홍길동 <hr>");
            // document.write("1. 홍길동 <hr>");
            // document.write("1. 홍길동 <hr>");
            // document.write("1. 홍길동 <hr>");
              
            // 1. 초기식 생성(변수 선언)
            // 2. 조건식으로 이용
             // 2-1. true -> 코드블럭 실행
             //      증가식으로 이동
             // 2-2. false -> 반복 종료
            // 3. 조건식(2번으로) 이동
            /*
            var i = 0;
            for (i = 1; i <= 100; i++) {
                document.write(i + ". 홍길동 <hr>");
            }
            */

            // 짝수(2의 배수) 번호만 찍기
            var i = 0;
            // 첫 번째 방법
            // for (i = 1; i<=100; i++) {
            //     if (i%2==0) {
            //         document.write(i + ". 홍길동 <hr>");
            //     }
            // }

            // 두 번째 방법
            for (i = 2; i <= 100; i+=2) {
                document.write(i + ". 홍길동 <hr>");
            }

            


        </script>
    </boby>
    </html>
profile
Engineering is the closest thing to magic that exists in the world.

0개의 댓글