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 Beginning C++ Programming now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.