Control flow

4.2 - Else statement


The else statement is a complement to the if statement. It is used like this:

if (condition) {
    // Do stuff if condition is true
}
else {
    // Do stuff if condition is false
}

It can also be used alongside if statements to check for many conditions, for example:

if (i > 0) {
    std::cout << i << " is a positive number." << std::endl;
}
else if (i < 0) {
    std::cout << i << " is a negative number." << std::endl;
}
else {
    std::cout << i << " is 0." << std::endl;
}

Task

Your task is to write a program using if and else statement to print out the color of the flowers as shown below:

flowercolor
rosered
violetblue
sunfloweryellow

Sample input

rose

Sample output

red
#include <iostream> int main() { std::string flower; std::cin >> flower; // Start your code here. }