Using Conditional Statements

The most frequently used conditional statement is if. In its simplest form, the if statement takes a logical expression in a pair of parentheses and is immediately followed by the statement that is executed if the condition is true:

    int i;     std::cin >> i;     if (i > 10) std::cout << "much too high!" << std::endl;

You can also use the else statement to catch occasions when the condition is false:

    int i;     std::cin >> i;     if (i > 10) std::cout << "much too high!" << std::endl;     else        std::cout << "within range" << std::endl;

If you want to execute several statements, you can use braces ({}) to define a code block.

The condition is a logical expression and C++ will convert from numeric types to a bool, where 0 is ...

Get Modern C++: Efficient and Scalable Application Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.