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

Get Sams Teach Yourself C++ in 24 Hours, Third Edition 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.