for, while, do while 반복문

서재환·2022년 2월 14일
0

JavaScript

목록 보기
16/25
const arr = ['a', 'b', 'c', 'd'];

const obj = {
  color = 'red',
  width = 200,
  height = 200,
}

for (let i = 0; i < arr.length; i++) {
    console.log(arr[i]);
}// a, b, c, d

for (const item of arr) {
  console.log(item);
} // a, b, c, d

for (const index in arr) {
  console.log(arr[index]);
} // a, b, c, d

for (const key in obj) {
  console.log(key);
} // color, width, height

let i = 0;

while (i < arr.length)
    console.log(arr[i++]);

i = 0;

do {
    console.log(arr[i++]);
} while (i < arr.length)
while 문과 do while 문의 차이점은 비교하는 시점이 서로 다른 부분이다. 

while 문의 경우 비교하는 시점이 while 안에서 부터 시작된다면 do while의 경우 먼저 명령어를 실행하고 나서
그 다음 아래 명령어를 실행할지 실행하지 말지에 대한 판단을 하게 된다.

0개의 댓글