if Statement in a C++ ProgramSometimes we want a program to select an action from two or more alternatives. This requires a deviation from the basic sequential order of statement execution. Such programs must contain two or more statements that might be executed, but have some way to select only one of the listed options each time the program is run. We accomplish this by using a control structure known as selection. Similar to the way in which a computer can perform arithmetic operations such as addition and calculate a numeric result from an arithmetic expression such as 2+4, a computer also can calculate a Boolean (logical: true or false) result from a relational expression such as 2<4. The result of that expression would be TRUE. A relational expression of 2>4 would have a result of FALSE. The result of the expression X<0 would depend on the value stored in variable X. The C++ language offers us the following relational operators to use to state conditions that must be tested within programs in order to select from alternative actions:
< meaning less than> meaning greater than== meaning equal to!= meaning not equal to<= meaning less than or equal to>= meaning greater than or equal toWhen designing programs that use selection, we can illustrate the selection structure
through either an [outlined algorithm] or a diagram
called a flowchart (see below). Such diagrams show the
flow of control
(or order of execution) of the steps within a program and can be used instead of an outline when developing
and documenting an algorithm. A complete example involving the use of a flowchart to solve a problem involving
the selection structure can be viewed on the web page entitled An Example
of a Problem Involving the Selection Structure. Students are also advised to read the basic web page about
Flowcharting Symbols & Guidelines.
The partial flowchart (a.k.a. a "flowchart segment") below illustrates
a selection structure based on the value stored in a variable named A.
The diamond shape encloses the condition that is being tested.
The processes leading out of the diamond are called branches or
legs and must be labeled to indicate which one is to be
followed when the condition in the diamond is TRUE or FALSE.
Selection structures such as the one above are coded in C++ using the reserved word
if. The general syntax of an if statement is:
if ( condition_in_the_diamond ) statement_to_execute_if_condition_is_true ; else statement_to_execute_if_condition_is_false ;
The indentation and line spacing above are ignored by the compiler and are simply
a matter of style. It is the use of reserved words and punctuation
that are significant. It is important to remember that an if statement in C++ can execute only
one statement on each leg (T or F). If we desire that multiple statements be executed
on a leg, we must block them inside of a
{ and } brace pair to make them
a single compound statement. Thus, the C++ code for the flowchart segment above
would be:
if (A>0 && A<9)
X=X*3;
else
{
X=1;
A=0;
}
Note the use of the parentheses around the condition in the code above.
Additional parentheses are often necessary because of the
order of precedence of operators in C+.
Also note the use of the double ampersand (&&) to
indicate the use of the logical operation and.
Aside from the relational operators listed above that are used to
compare values, C++ also offers us a few logical operators
(a.k.a Boolean operators) to combine relational expressions into more complex
compound logical expressions such as the one above in which two relationships
must exist: A>0 and A<9. C++ provides the following logical operators to
combine relational expressions (abbreviated below as rel_expr).
!(rel_expr) meaning notrel_expr && rel_expr meaning andrel_expr || rel_expr meaning orNote that relational expressions always produce Boolean (true or false) results and logical operators must be given Boolean operands to evaluate. Thus, it would be illegal syntax in C++ to use the expression
A=='Y' || 'y'
to check for either the uppercase or lowercase letter "Y" in variable A.
The correct syntax would be
A=='Y' || A=='y'
Notice that the first (illegal) statement does not have Boolean data on the right side of the || (or) operator, but the second (legal) statement has Boolean data on both sides of the || operator.
Remember also that the false leg of a selection structure can be empty, but not the true leg. If your flowchart shows the true leg as empty, then reverse the labeling of the true and false legs and reverse the logic of the condition inside the diamond. If you can't determine what the logical opposite of the condition would be, simply use the NOT operator like this:
! ( original_condition )