Predict the program output

Predict the program output

Use your knowledge of C++ to predict what this program will output.

#include <iostream>

int main() 
{
    int value = 4;

    if (value < 5)
        std::cout << "A";
    else
        std::cout << "B";
    std::cout << "C";
}

Answer

The output is AC. When there aren't any curly brackets following an if statement the next statement is tied to it. The code below is the equivalent logic, but with curly brackets.

#include <iostream>

int main() 
{
    int value = 4;

    if (value < 5) 
    {
        std::cout << "A";
    }
    else 
    {
        std::cout << "B";
    }
    std::cout << "C";
}
Questions