C++ 그렇지 않으면


else if 문

else if첫 번째 조건이 인 경우 명령문을 사용하여 새 조건을 지정 하십시오 false.

통사론

if (condition1) {
  // block of code to be executed if condition1 is true
} else if (condition2) {
  // block of code to be executed if the condition1 is false and condition2 is true
} else {
  // block of code to be executed if the condition1 is false and condition2 is false
}

예시

int time = 22;
if (time < 10) {
  cout << "Good morning.";
} else if (time < 20) {
  cout << "Good day.";
} else {
  cout << "Good evening.";
}
// Outputs "Good evening."

설명된 예

위의 예에서 시간(22)은 10보다 크므로 첫 번째 조건false입니다. else if명령문 에서 다음 조건 도 이므로 condition1condition2 가 모두 이므로 조건 false으로 이동 하고 화면에 "좋은 저녁"을 인쇄합니다.elsefalse

그러나 시간이 14일 경우 프로그램은 "Good day"를 인쇄합니다.