제어 구조: 조건부 및 루프
JavaScript에는 조건 및 루프에 대한 여러 유형의 제어 구조가 있습니다. 조건:.
JavaScript에는 조건 및 루프에 대한 여러 유형의 제어 구조가 있습니다.
조건:
- If 문: JavaScript의 if 문은 다른 프로그래밍 언어와 유사합니다. 조건을 평가하고 조건이 참이면 코드 블록을 실행합니다. 예:
- While 루프: JavaScript의 while 루프는 지정된 조건이 참인 동안 코드 블록을 실행합니다. 예:
if (x > 0) {
console.log("x is positive");
}
if (x > 0) {
console.log("x is positive");
} else {
console.log("x is not positive");
}
switch (color) {
case "red":
console.log("The color is red");
break;
case "blue":
console.log("The color is blue");
break;
default:
console.log("The color is not red or blue");
break;
}
while (i < 5) {
console.log(i);
i++;
}
do {
console.log(i);
i++;
} while (i < 5);
for (var i = 0; i < 5; i++) {
console.log(i);
}