반복문은 동일한 동작을 원하는 횟수만큼 조건을 만족할 때까지 반복해서 수행한다.
for (초기상태; 조건; counter 변화) {
수행할 동작
};
for (초기상태; 반복조건; 반복이 될 때마다 실행되는 코드) {
반복해서 실행될 코드
}
for (let i = 0; i < 10; i++) {
console.log('Hello world');
};
for (let step = 0; step < 10; step++) {
console.log('Hello Sejin!');
};
// 빈 배열에 100부터 110까지 요소를 추가하기
// 배열의 요소 추가하는 방법은 push
let numArray = [];
for (let i = 100; i <= 110; i++) {
//console.log('before push: ', numArray);
numArray.push(i);
//console.log('after push: ', numArray);
};
console.log(numArray);
let colors = ['red', 'blue', 'yellow', 'green', 'black', 'white'];
for (let i = 0; i < colors.length; i++) {
console.log(colors[i]);
}
오늘 공부한 반복문은 대표적인 for문만 공부했지만, 반복문에는 while문도 있고, for in문도 있고 다양하게 있다. 이번 주에 반복문과 조건문에 대해서 더 공부를 해야겠다!!!ㅎㅎㅎ