O'Reilly logo

Beginning C++ Programming by Richard Grimes

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required