Monday, January 17, 2011

If statements

The most basic kind of conditional branch in C++ is the if statement. An if statement takes the form:
if (expression)
    statement
or
if (expression)
    statement
else
    statement2
If 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:
01#include <iostream>
02
03int main()
04{
05    using namespace std;
06    cout << "Enter a number: ";
07    int nX;
08    cin >> nX;
09
10    if (nX > 10)
11        cout << nX << "is greater than 10" << endl;
12    else
13        cout << nX << "is not greater than 10" << endl;
14
15    return 0;
16}
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:
01#include <iostream>
02
03int main()
04{
05    using namespace std;
06    cout << "Enter a number: ";
07    int nX;
08    cin >> nX;
09
10    if (nX > 10)
11        {
12        // both statements will be executed if nX > 10
13        cout << "You entered " << nX << endl;
14        cout << nX << "is greater than 10" << endl;
15        }
16    else
17        {
18        // both statements will be executed if nX <= 10
19        cout << "You entered " << nX << endl;
20        cout << nX << "is not greater than 10" << endl;
21        }
22
23    return 0;
24}
It is possible to chain if-else statements together:
01int main()
02{
03    using namespace std;
04    cout << "Enter a number: ";
05    int nX;
06    cin >> nX;
07
08    if (nX > 10)
09        cout << nX << "is greater than 10" << endl;
10    else if (nX < 5)
11        cout << nX << "is less than 5" << endl;
12    // could add more else if statements here
13    else
14        cout << nX << "is between 5 and 10" << endl;
15
16    return 0;
17}
It is also possible to nest if statements within other if statements:
01#include <iostream>
02
03int main()
04{
05    using namespace std;
06    cout << "Enter a number: ";
07    int nX;
08    cin >> nX;
09
10    if (nX > 10)
11        // it is bad coding style to nest if statements this way
12        if (nX < 20)
13            cout << nX << "is between 10 and 20" << endl;
14
15        // who does this else belong to?
16        else
17            cout << nX << "is greater than 20" << endl;
18
19    return 0;
20}
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:
01#include <iostream>
02
03int main()
04{
05    using namespace std;
06    cout << "Enter a number: ";
07    int nX;
08    cin >> nX;
09
10    if (nX > 10)
11    {
12        if (nX < 20)
13            cout << nX << "is between 10 and 20" << endl;
14        else // attached to inner if statement
15            cout << nX << "is greater than 20" << endl;
16    }
17
18    return 0;
19}
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:
01#include <iostream>
02
03int main()
04{
05    using namespace std;
06    cout << "Enter a number: ";
07    int nX;
08    cin >> nX;
09
10    if (nX > 10)
11    {
12        if (nX < 20)
13            cout << nX << "is between 10 and 20" << endl;
14    }
15    else // attached to outer if statement
16        cout << nX << "is less than 10" << endl;
17
18    return 0;
19}
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:
01#include <iostream>
02#include <cmath> // for sqrt()
03
04void PrintSqrt(double dValue)
05{
06    using namespace std;
07    if (dValue >= 0.0)
08        cout << "The square root of " << dValue << " is " << sqrt(dValue) << endl;
09    else
10        cout << "Error: " << dValue << " is negative" << endl;
11}
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.
01int DoCalculation(int nValue)
02{
03    // if nValue is a negative number
04    if (nValue < 0)
05       // early return an error code
06        return ERROR_NEGATIVE_NUMBER;
07
08    // Do calculations on nValue here
09
10    return nValue;
11}
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:
1int min(int nX, int nY)
2{
3    if (nX > nY)
4        return nY;
5    else
6        return nX;
7}
Note that this last function is so simple, it can also be written using the arithmetic if operator (?:):
1int min(int nX, int nY)
2{
3    return nX > nY ? nY : nX;
4}

No comments: