O'Reilly logo

Sams Teach Yourself C++ in 24 Hours, Third Edition by Jesse Liberty

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

The if Statement

Normally, your program flows along line by line in the order that it appears in your source code. The if statement enables you to test for a condition (such as whether two variables are equal) and branch to different parts of your code depending on the result.

The simplest form of an if statement is this:

if (expression)
     statement;

The expression in the parentheses can be any expression at all, but it usually contains one of the relational expressions. If the expression has the value zero, it is considered false, and the statement is skipped. If it has any nonzero value, it is considered true, and the statement is executed. Look at this example:

if (bigNumber > smallNumber)
     bigNumber = smallNumber;

This code compares bigNumber ...

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