LESSON 5 Making Decision
LESSON 5 Making Decision
Making Decisions
Topics:
Relational operators allows you to compare numeric and char values and determine whether one is greater
than, less than, equal to, or not equal to another.
All of the relational operators are binary, which means they use
two operands. Here is an example of an expression using the
greater-than operator:
x > y
The table below shows examples of several relational expressions that compare the
variables x and y.
5.1Relational Operators
These expressions are true because a is not equal to b and b is not equal
to c. But the following expression is false because a is equal to c:
a != c
5.1Relational Operators
Example 1 Example 2
if (it is raining) if (you have a test tomorrow)
wear a rain coat study tonight
bring an umbrella
else
watch a movie
In this example, the portion in parentheses, called the condition, specifies the
decision you are making and is phrased so that it results in either a true or false answer
only.
If the condition is true, you will perform a specific set of tasks. If the condition is
false, on the other hand, you might not need to perform a different set of tasks.
5.2 The if statement
Example 1 Example 2
if (it is raining) if (you have a test tomorrow)
wear a rain coat study tonight
bring an umbrella
else
watch a movie
For instance, look at the first example, if it is raining (a true condition), then you
will wear a raincoat and bring an umbrella. Notice that you do not have anything in
particular to do if it is not raining (a false condition).
5.2 The if statement
Example 1 Example 2
if (it is raining) if (you have a test tomorrow)
wear a rain coat study tonight
bring an umbrella
else
watch a movie
Compare this with the second example, if you have a test tomorrow (a true
condition), then you will study tonight. If you do not have a test tomorrow (a false
condition), however, you will watch a movie.
5.2 The if statement
One way to code a decision structure in C++ is with the if statement. Here is the general
format of the if statement:
The if statement is simple in the way it works: If the value of the expression inside the
parentheses is true, the very next statement is executed. Otherwise, it is skipped. The
statement is conditionally executed because it only executes under the condition that the
expression in the parentheses is true.
The if/else statement will execute one group of statements if the expression is true, or another
group of statements if the expression is false.
As with the if statement, an expression is evaluated. If the expression is true, a statement or block
of statements is executed. If the expression is false, however, a separate group of statements is
executed.
Program 5-4
5.3 The if else statement
The else part at the end of the if statement specifies a statement that is to be executed
when the expression is false. When number % 2 does not equal 0, a message is printed indicating
the number is odd. Note that the program will only take one of the two paths in the if/else
statement. If you think of the statements in a computer program as steps taken down a road,
consider the if/else statement as a fork in the road. Instead of being a momentary detour, like
an if statement, the if/else statement causes program execution to follow one of two exclusive
paths. The flowchart in picture below shows the logic of this if/else statement.
To test more than one condition, an if statement can be nested inside another if statement.
To test more than one condition, an if statement can be nested inside another if statement.
The if/else if statement tests a series of conditions. It is often simpler to test a series of conditions with the if/else if
statement than with a set of nested if/else statements.
In C++, and many other languages, you can alternatively test a series of conditions using the if/else
if statement. The if/else if statement makes certain types of nested decision logic simpler to write. Here
is the general format of the if/else if statement:
5.5 The if/else if statement
Program 5-8
5.6 The Logical Operators
The && operator is known as the logical AND operator. It takes two expressions as
operands and creates an expression that is true only when both sub-expressions are
true.
NOTE: You must provide complete expressions on both sides of the &&
operator. For example, the following is not correct because the
condition on the right side of the && operator is not a complete
expression.
temperature > 0 && < 100
The expression must be rewritten as
temperature > 0 && temperature < 100
5.6 The Logical Operators
As the table shows, both sub-expressions must be true for the && operator to return a true
value.
5.6 The Logical Operators
NOTE: If the sub-expression on the left side of an && operator is false, the
expression on the right side will not be checked. Since the entire
expression is false if only one of the sub-expressions is false, it would
waste CPU time to check the remaining expression. This is called short
circuit evaluation.
The && operator can be used to simplify programs that otherwise would use nested if
statements.
Program Example
5.6 The Logical Operators
THE || OPERATOR
THE || OPERATOR
THE || OPERATOR
THE || OPERATOR
The table below shows a truth table for the || operator.
All it takes for an OR expression to be true is for one of the sub-expressions to be true. It
doesn’t matter if the other sub-expression is false or true.
5.6 The Logical Operators
THE || OPERATOR
NOTE: The || operator also performs short circuit evaluation. If the sub-
expression on the left side of an || operator is true, the expression
on the right side will not be checked. Since it’s only necessary for one
of the sub-expressions to be true, it would waste CPU time to check
the remaining expression.
Program 5-10
5.6 The Logical Operators
THE ! OPERATOR
The ! operator performs a logical NOT operation. It takes an operand and
reverses its truth or falsehood. In other words, if the expression is true, the ! operator
returns false, and if the expression is false, it returns true. Here is an if statement using
the ! operator:
THE ! OPERATOR
if (!(temperature > 100))
cout << "You are below the maximum temperature.\n";
First, the expression (temperature > 100) is tested to be true or false. Then the !
operator is applied to that value. If the expression (temperature > 100) is true, the !
operator returns false. If it is false, the ! operator returns true. In the example, it is
equivalent to asking “is the temperature not greater than 100?”
5.6 The Logical Operators
THE ! OPERATOR
The table below shows a truth table for the ! operator.
Program 5-11
5.7 The Switch statement
After the word case is a constant expression (which must be of an integer type),
followed by a colon. The constant expression may be an integer literal or an integer
named constant. The case statement marks the beginning of a section of statements. The
program branches to these statements if the value of the switch expression matches that
of the case expression.
WARNING! The expression of each case statement in the block
! must be unique.
NOTE: The expression following the word case must be an
integer literal or constant. It cannot be a variable,
and it cannot be an expression such as x < 22 or
n == 50.
An optional default section comes after all the case statements. The program branches to this
section if none of the case expressions match the switch expression. So, it functions like a trailing else in
an if/else if statement.
5.7 The Switch statement
The case statements show the program where to start executing in the block and the
break statements show the program where to stop. Without the break statements, the program
would execute all of the lines from the matching case statement to the end of the block.
5.7 The Switch statement
NOTE: The default section (or the last case section, if there is no
default) does not need a break statement. Some
programmers prefer to put one there anyway, for
consistency.
Program 5-13 Without the break statement, the program “falls
through” all of the statements below the one with
the matching case expression. Sometimes this is what
you want.
When the user enters 'a' the corresponding case has no statements associated with it,
so the program falls through to the next case , which corresponds with 'A'.
case 'a':
case 'A': cout << "30 cents per pound.\n";
break;
The same technique is used for 'b' and 'c'.