Conditional statements are used to perform different actions based on different conditions.
if StatementUse if to specify a block of code to be executed if a condition is true.
int time = 20;
if (time < 18) {
cout << "Good day.";
}
else StatementUse else to specify a 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.
else if StatementUse else if to specify a new condition if the first condition is false.
int time = 22;
if (time < 10) {
cout << "Good morning.";
} else if (time < 20) {
cout << "Good day.";
} else {
cout << "Good evening.";
}
By combining these statements, your programs can make decisions on the fly!
Which statement is used to specify a new condition if the first condition is false?