C ++ Else


elseステートメント

ステートメントを使用してelse、条件が。の場合に実行されるコードのブロックを指定しますfalse

構文

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

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

例の説明

上記の例では、時間(20)は18より大きいため、条件はfalseです。このため、条件に移り、else「こんばんは」画面に印刷します。時間が18未満の場合、プログラムは「Goodday」を出力します。