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