: 같은 작업을 같은 자리에서 반복
: 몇 바퀴 돌려야할 지 알 때 주로 사용
for(let i = 0; i < 3; i++){
console.log(i);
}
-> 0
1
2let obj = {x:10, y:11, z:12};
for(property in obj){
console.log("name : " + property + ", value : " + obj[property]);
}
-> name : x, value : 10
name : y, value : 11
name : z, value : 12let arr = {100, 200, 300};
for(let value of arr){
console.log(value);
}
-> 100
200
300let arr = {100, 200, 300};
arr.forEach(function(item, index){
console.log(item, index)
});
-> 100 1
200 2
300 3: 몇 바퀴 돌릴지 모를 때 주로 사용
초기식;
while(조건식;){
조건식이 true일 때 실행할 코드;
증감조건;
}
--------
let i = 0;
while(i<10){
console.log(i);
i++;
}
: 최소 1번은 돌릴 때 사용
원치않을 때도 돌리니까 잘 사용해야 함.
초기식;
do{
첫 실행 + 조건식이 true일 때 실행할 코드;
증감조건;
}while(조건식;)
for(let i = 1; i < 5; i++){
console.log(i);
if(i == 3){
break;
}
console.log("for");
}
-> 1for
2for
3
for(let i = 1; i < 5; i++){
console.log(i);
if(i == 3){
continue;
}
console.log("for");
}
-> 1for
2for
3
4for
for(let i = 0; i < 5; i++){
for(let j = 0; j < 5; j++){
for(let k = 0; k < 5; k++){
for(let q = 0; q < 5; q++){
}
}
}
}