Use the else
statement to specify a block of code to be executed if the condition is 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."
Try it Yourself »
In the example above, time (20) is greater than 18, so the condition is false
. Because of this, we move on to the else
condition and print to the screen "Good evening". If the time was less than 18, the program would print "Good day".
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!