()
안에 조건을 선언한다. {}
에 실행할 코드를 적는다.do { }
코드블록을 먼저 실행하고while ( )
조건이 평가된다.const cupsOfSugarNeeded = 4;
let cupsAdded = 0;
do {
cupsAdded++;
console.log(cupsAdded);
} while (cupsAdded < cupsOfSugarNeeded);
// 1
2
3
4 출력
break 키워드는 실행중인 루프블럭을 나갈 때 사용한다. 큰 데이터구조를 반복할 때 도움이 될 수 있다.
const rapperArray = ["Lil' Kim", "Jay-Z", "Notorious B.I.G.", "Tupac"];
for (let i = 0; i < rapperArray.length; i++) {
console.log(rapperArray[i]);
if (rapperArray[i] === 'Notorious B.I.G.') {
console.log(rapperArray[i]);
break;
}
} console.log('And if you don\'t know, now you know.');
// 출력
Lil' Kim
Jay-Z
Notorious B.I.G.
Notorious B.I.G.
And if you don't know, now you know.
// 0에서 10까지 반복, 8까지만 도달하게 하기
for (i = 0; i <= 10; i++) {
if (i > 8) {
break;
}
console.log(i);
}
// 0에서 10까지 반복, 짝수만 프린트하기(홀수인 경우 스킵)
for (i = 0; i <= 10; i++) {
if (i % 2 == 1) {
continue;
}
console.log(i);
}