if Statement in a C++ Program
before reading the following text.When faced with a situation in which a program must select from many processing
alternatives based on the value of a single variable, an analyst must expand his or her
use of the basic selection structure beyond the standard two processing branches offered
by the if statement to allow for multiple branches. One solution to this is
to use an approach called nesting in which one (or both) branch(es) of a selection
contain another structure (such as selection). This approach is applied to each branch of an algorithm
until enough additional branches have been created to handle each alternative.
For example, consider the following problem that involves the use of a menu of actions.
Display a numbered menu involving the use of the terms "single", "double", "triple", and "quadruple" to help young students understand them. Allow the student (user) to select one of the words by entering the number associated with it on the menu, and then display a word indicating the multiplier associated with the term selected.
The numerals on the left of the sample output below are there for the analyst's reference only and will not appear on the screen. Underlined items indicate values entered by the user.
1 2 3 4 5 6 7 8 9 10 |
|
| Label | Description | Data Type | Source | Usage | Destination |
|---|---|---|---|---|---|
| N | Menu choice (1-4) | Integer | Keyboard | case selector | --- |
The flowchart below (or the alternative [outlined algorithm]) uses multiple if structures
(each with only two legs), nested one within another on the false leg (branch)
from each test to allow for enough multiple legs to solve the problem.
Notice that the final false (else) leg does not contain another if
structure, but rather handles the possibility that a user might enter a
menu response that is outside of the range of acceptable values by simply
displaying the word "ERROR" in place of an appropriate word.
Certainly, a more user-friendly approach could have been used, but it would
have increased the complexity of this example.

Because this program has five paths through its algorithm (as illustrated
in the flowchart above), an analyst would typically perform and document five
desk checks involving carefully selected test data (menu choice values for N)
that would result in testing each of those five paths. The Tracing Chart
for only one path (resulting form menu choice 2) is provided below to illustrate
the documentation involved in tracking multiple tests performed when using
nested if structures. The other documentation should be obvious.
Note that some of the columns in this chart have no data because the tests
that they track were not performed as a result of the test data value being 2.
| N | N = 1 | N = 2 | N = 3 | N = 4 |
|---|---|---|---|---|
| 2 | ||||
| False | ||||
| True |
/* menu.cpp - an example of nested if selection */
/* Written by: Randolph Gibson - Date: Feb. 18, 2011 */
#include <iostream>
using namespace std;
int main ()
{
int N; /* Menu Choice */
cout << "MENU OF TERMS\n\n";
cout << "1. Single\n";
cout << "2. Double\n";
cout << "3. Triple\n";
cout << "4. Quadruple\n\n";
cout << "Enter the number of the term you want explained (1-4)? ";
cin >> N;
cout << "\nThat word means to multiply by ";
if (N == 1) cout << "one";
else if (N == 2) cout << "two";
else if (N == 3) cout << "three";
else if (N == 4) cout << "four";
else cout << "ERROR";
cout << ".\n";
return 0;
}
The use of the indentation in the code above is not necessary, but does improve readability.
Solutions using nested if structures like the one above
may work, but can become extremely difficult to flowchart and code when
they involve many alternatives (paths). Imagine how involved the
documentation would be for a menu-oriented program that offered 20 choices.
An alternative approach known as Case Selection can be used to
document selective situations like the one in the example above
where multiple processing paths are required as the result of the
value of a single variable.
This approach has restrictions. Case Selection can be used only in situations
where the branching is based entirely on the value of a
single variable and where those values are part of a set of
ordinal data. Understand that the use of the Case
Selection structure is never necessary, because the situations it solves can
always be handled using nested if statements as shown above.
Its value is in making the documentation easier to read.
The flowchart below (or the alternative [outlined algorithm])
shows the same solution to the problem in the example
above, but using the Case Selection structure. This document
is smaller and easier to follow. It more clearly conveys the nature of the
selective process as involving a selection of one path as the result of
examining the value of a single variable. If you compare it to the first
algorithm showing the nested if approach, you will see the
weakness of the nested approach. That algorithm makes it appear that a
series of tests is required in some specific order to choose the
correct path through the program. This is not really the truth.
The algorithm for the Case Selection below makes no such implication.
It merely shows that a specific value of N will cause a particular
processing path to be taken.

Because this program has five paths through its algorithm (as illustrated in the flowchart above), an analyst would typically perform and document five desk checks involving carefully selected test data (menu choice values for N) that would result in testing each of those five paths. As in the earlier example, the Tracing Chart for only one path (resulting form menu choice 2) is provided below to illustrate the documentation involved in tracking multiple tests performed when using a Case Selection structures. Note that this chart requires no columns for tests related to N's value, as that value is already being tracked in the column devoted to that variable. The analyst would know which legs were followed based on the labels atop each leg in the case selection on the flowchart. Thus the Tracing Chart is vastly simplified.
| N |
| 2 |
That's it; a big improvement!
/* menu.cpp - an example of case selection */
/* Written by: Randolph Gibson - Date: Feb. 18, 2011 */
#include <iostream>
using namespace std;
int main ()
{
int N; /* Menu Choice */
cout << "MENU OF TERMS\n\n";
cout << "1. Single\n";
cout << "2. Double\n";
cout << "3. Triple\n";
cout << "4. Quadruple\n\n";
cout << "Enter the number of the term you want explained (1-4)? ";
cin >> N;
cout << "\nThat word means to multiply by ";
switch (N)
{
case 1: cout << "one"; break;
case 2: cout << "two"; break;
case 3: cout << "three"; break;
case 4: cout << "four"; break;
default: cout << "ERROR"; break;
}
cout << ".\n";
return 0;
}
As you can see by comparing the switch statement with the nested
if statements in the previous C++ source code, the code for the case
selection is easier to read and more clearly communicates the process being
performed. The clarity of the Case Selection is the reason that almost all
modern programming languages have syntax to implement this structure.
Its only weakness is its use of four separate keywords for implementation.
switch - starts the structure and defines the single variable being testedcase - starts a single leg and defines the value or values that select itbreak - ends a leg and forces a branching ahead to the closing bracedefault - starts the default leg that will be followed if an unspecified value is testedBe aware that the syntax and keywords used to implement the Case Selection structure are often quite different in other programming languages.
Click here for more examples of coding the case selection structure.