JavaScript(10)

박찬영·2024년 1월 18일

JavaScript

목록 보기
10/19

1. 반복문

반복문은 비슷하거나 동일한 구문을 반복해서 수행할 수 있는 구문이다.
대표적인 반복문은 다음 두 가지이며, 두 반복문은 구조 및 동작방식에서 차이를 보인다.

• while문
• for문

2. while문

키워드 while을 이용해 만드는 구문 while문은 '주어진 조건이 참일 동안에 구문을 반복하는' 반복문이다. 
while문의 기본 형태는 다음과 같다.

3. for문

for문은 구문 작성시 반복을 위해 필요한 세 가지 요소를 한 곳에 모아 작성함으로써 보다 명시적으로 
반복 횟수를 표현할 수 있는 직관적인 구문이다.

→ 초기식: 반복 조건의 초기화 작업
   반복식: 반복이 한 번 끝날 때마다 실행될 작업
   
   
   

4. 실습

1) html 코드

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE-edge">
        <meta name="viewport" content="width=device-width, inital-scale=1">
        <title>자바스크립트 실습</title>
    </head>
    <body>

        <script src="script.js"></script>
    </body>
</html>

2) js 코드

let number02 = 1;

while(number02 < 10){
    console.log(number02);
    number02 += 1;
}


while(confirm("메세지!")){
    console.log("확인 버튼을 누르고 있군요")
}
console.log("취소 버튼을 눌렀군요")


for(let i=1; i<=8; i+=1){
    if(i%2==0){
        console.log(i)
    }
}

for(let i=8; i>=1; i-=2){
    console.log(i)
}

3) 결과

profile
블로그 이전했습니다 -> https://young-code.tistory.com

0개의 댓글