본문으로 건너뛰기
Advertisement

C: 조건문 (Conditional Statements)

상황에 따라 다른 코드를 실행하도록 하는 제어문입니다.

1. if / else

int score = 85;

if (score >= 90) {
printf("A 학점\n");
} else if (score >= 80) {
printf("B 학점\n");
} else {
printf("기타\n");
}

2. switch / case

특정한 값과 일치하는 경우를 선택할 때 사용합니다.

int day = 2;

switch (day) {
case 1:
printf("월요일\n");
break;
case 2:
printf("화요일\n");
break;
default:
printf("기타 요일\n");
}

정보

switch문에서 break를 생략하면 다음 case까지 이어서 실행되니 주의해야 합니다.

Advertisement