The most basic kind of conditional branch in C++ is the
if statement. An
if statement takes the form:
if (expression)
statementor
if (expression)
statement
else
statement2If the expression evalutes to true (non-zero), the statement executes. If the expression evaluates to false, the
else statement is executed if it exists.
Here is a simple program that uses an
if statement:
06 | cout << "Enter a number: "; |
11 | cout << nX << "is greater than 10" << endl; |
13 | cout << nX << "is not greater than 10" << endl; |
Note that the
if statement only executes a single statement if the expression is true, and the
else only executes a single statement if the expression is false. In order to execute multiple statements, we can use a block:
06 | cout << "Enter a number: "; |
13 | cout << "You entered " << nX << endl; |
14 | cout << nX << "is greater than 10" << endl; |
19 | cout << "You entered " << nX << endl; |
20 | cout << nX << "is not greater than 10" << endl; |
It is possible to chain if-else statements together:
04 | cout << "Enter a number: "; |
09 | cout << nX << "is greater than 10" << endl; |
11 | cout << nX << "is less than 5" << endl; |
14 | cout << nX << "is between 5 and 10" << endl; |
It is also possible to nest if statements within other if statements:
06 | cout << "Enter a number: "; |
13 | cout << nX << "is between 10 and 20" << endl; |
17 | cout << nX << "is greater than 20" << endl; |
The above program introduces a source of potential ambiguity called a
dangling else problem. Is the
else statement in the above program matched up with the outer or inner
if statement?
The answer is that an
else statement is paired up with the last unmatched
if statement in the same block. Thus, in the program above, the
else is matched up with the inner
if statement.
To avoid such ambiguities when nesting complex statements, it is generally a good idea to enclose the statement within a block. Here is the above program written without ambiguity:
06 | cout << "Enter a number: "; |
13 | cout << nX << "is between 10 and 20" << endl; |
15 | cout << nX << "is greater than 20" << endl; |
Now it is much clearer that the
else statement belongs to the inner
if statement.
Encasing the inner
if statement in a block also allows us to explicitly attach an
else to the outer
if statement:
06 | cout << "Enter a number: "; |
13 | cout << nX << "is between 10 and 20" << endl; |
16 | cout << nX << "is less than 10" << endl; |
The use of a block tells the compiler that the
else statement should attach to the
if statement before the block. Without the block, the
else statement would attach to the nearest unmatched
if statement, which would be the inner
if statement.
If statements are commonly used to do error checking. For example, to calculate a square root, the value passed to the square root function should be a non-negative number:
02 | #include <cmath> // for sqrt() |
04 | void PrintSqrt(double dValue) |
08 | cout << "The square root of " << dValue << " is " << sqrt(dValue) << endl; |
10 | cout << "Error: " << dValue << " is negative" << endl; |
If statements can also be used to do
early returns, where a function returns control to the caller before the end of the function. In the following program, if the parameter nValue is negative, the function returns a symbolic constant or enumerated value error code to the caller right away.
01 | int DoCalculation(int nValue) |
06 | return ERROR_NEGATIVE_NUMBER; |
If statements are also commonly used to do simple math functionality, such as a min() or max() function that returns the minimum or maximum of it’s parameters:
Note that this last function is so simple, it can also be written using the arithmetic if operator (?:):
3 | return nX > nY ? nY : nX; |
No comments:
Post a Comment