Section IV. Branch Statements
| Section IV : Branch Statements | Section I: Variables | Section II: Our First Program | Section III: Iteration |
|
Table of Contents
Learning C++:
An Index of Entry Points
2. The A reference document on the basic elements of C++.
3. The Patterns
|
A. The Basic
If...Else In other words, do the first set of
instructions if the person has worked more than 40 hours (a typical
definition for overtime) and another set if the person has not worked more
than 40 hours. Just as the 'while' loop construct uses a test condition to
decide whether to execute the code inside the loop, the code for the 'if'
construct executes a test to determine which code branch to take.
Let's start with the simple, non-looping form of the employee salary
problem found in section II-E
above and add the necessary code to calculate overtime pay: You can see the
'if' statement here but before we discuss it, let's take a look at another
item introduced, the named constant.
The lines: act like variable declarations (and definitions) with immediate
initialization. The difference is that, because of the word const,
these memory locations cannot have their values changed! It would be
illegal given this code to write, for example: elsewhere in this program.
Whenever there is a value that does not change in a program it can be
called a constant. We have already used a number of string
constants as in:
The two values 40 and 1.5 in the above program clearly do not change
and are referred to as numeric constants. Note that neither the
string constant "Your salary is" nor the numeric constants '40' or '1.5'
have identifier names. In fact, they do not have any computer memory
locations associated with them. However, 'REGULAR_HOURS' and
'OVERTIME_ADJUSTMENT' are identifiers and they represent symbolic names
for specific memory locations. Since these two identifiers cannot be
changed but they refer to specific memory locations with symbolic names,
they are referred to as 'named constants'.
These two identifiers can be used anywhere a numeric constant can be
used, for example, on the right side of arithmetic expressions or in
comparisons. In such cases, any references to 'REGULAR_HOURS' or
"OVERTIME_ADJUSTMENT' in the code will use the values '40' and 1.5'
respectively.
It is usually wise not to use numeric constants (such as 40 and 1.5)
directly in your code. Instead, you should uniquely identify any values
that do not change in your program - your numeric constants - as named
constants. There are two reasons for this. First, we talked above about
providing good names for our variables, about how that makes our programs
more readable. If you include a number in a program, that number does not
explain itself. The values '40' and 1.5' have no meaning in themselves.
Someone who knew that 40 was a value commonly used as the difference
between regular and overtime work might recognize its use in this program
but it would not be self-explanatory.
Imagine a more complex piece of code that used the value 1.324 to
represent the salary adjustment for employees who work the night shift. It
is most unlikely that anyone outside of the personnel department would
know the meaning of 1.324. Since often programmers are not familiar with
the intricacies of the department or organization for which they are
coding, any code that used the value 1.324 in this way would not be
intuitively readable. On the other hand, if the code had a reference to
'NIGHT_SHIFT_PAY_ADJUSTMENT', any programmer reading this code would have
some sense of what this was about. He or she may not understand exactly
how night shift pay is calculated, but they would know, at least in a
general sense, what the code was attempting to do. As mentioned earlier,
one should always strive to write code that is as readable as possible.
That is reason number
one. The second reason for using 'named constants' involves potential
changes to code. For example, someday the United States will realize that
the benefits of factory automation need to be shared with employees and
the regular work week will be reduced to say 30 hours. At that time the
program we just wrote will need to be changed. If we had written '40' in
all the places where the letters 'REGULAR_HOURS' appear, we would have to
change all those '40's to '30's. In this example, all the lines where the
value '40' would appear are near each other but imagine a much larger
program where uses of this same '40' are separated by 100's or 1000's of
code lines. The odds are very good that at least one of the 40's would not
get changed. However, if we declare REGULAR_HOURS' as a 'named constant'
with the value 40 and use it in all the relevant places, we only need to
make the change from 40 to 30 in one spot - the constant declaration. In
other words, to use '30' as the basis for calculating overtime pay in the
above program you only need change: to:
Note that the only difference between a 'named
constant' and a variable is that a 'named constant' cannot be changed once
it is declared. Such constants are also symbolic names for memory
locations and they can be used most any place a variable can be used if we
are only accessing, not changing, the value of the 'named constant'. (At
this point we shall stop making the distinction between numeric constants
such as '40' and 'named constants' such as REGULAR_HOURS. Both will be
referred to as constants unless the distinction is needed for
clarity.)
Although constants and variables are almost the same they are still
different. To emphasize that certain symbolic names represent constants
and not variables, we will write all 'named constants' in upper-case
letters with underbars to make multi-word constant names more readable. In
contrast, variable names are written in lower case except that in
multi-word variables the first letter of every word (except the first) is
in upper case. See the Style Sheet for more
details.
Back to the discussion of the 'if' statement and its use in calculating
salaries. Now we can focus on the code: In this case, the code is testing to see if the hours worked entered by
the user is greater than 40, the value in the constant REGULAR_HOURS.
If the answer is yes, the code in the brackets just after the test is
executed; if not, the code in the brackets just after the 'else' is
executed. When you stop to consider it, this is exactly the same way we
would use an 'if...else' statement in English. Don't read into this
construct something mysterious or complex.
Note further that there is no loop here. Either the code in the first
set of brackets is executed OR the code in the second set is executed but
not both and NOT over and over. Don't confuse the 'if' and 'while'
statements. They have quite different purposes.
The code inside the 'if' statement should be clear from our earlier
discussion of the arithmetic operators. The one new part is that the
single arithmetic expression:
As this code stands, it is not very useful because it only calculates
the salary for one employee. To allow this program to calculate the salary
for ten employees you need to add a loop similar to the ones we have
already seen. Inside that loop you place the code we just wrote. For
example: // A program to calculate the salaries for 10 employees //
File: ch2prg7.cpp#include <iostream.h>const int
REGULAR_HOURS = 40; const
double OVERTIME_ADJUSTMENT =
1.5; One minor point about this code: We have been skipping the 'endl' in
many of our programs because the programs only output one value. Here we
must include it in the line that outputs the salary or all the salary
statementswill appear on one ugly line.
More on the 'if' statement: As in English, it is possible to have an
'if' statement that does not include an 'else' part. A program can have
'if' and 'if...else' statements, but not simply 'else' statements. (Try
beginning a sentence in English with 'else'.)
As an example of code involving only an 'if', suppose you were askedto
write a program that simply counted the number of employees who workedmore
than 40 hours during the last pay period. Here is how such a programwould
look: B. The Relational Operators
The 'equal' symbol is used in comparisons. It essentially asks the
question- is the left side equal to the right side and the answer that
comes backis always C++'s equivalent of 'Yes' or 'No' / 'True' or 'False'.
You needto be careful to always use "==" within 'if' and 'while' test
conditionsor the results you get might surprise you.
Unfortunately, the compiler may give you a warning when you use '='
when you mean '==' but it will let you go ahead. (Compilers have at least
two levels of messages - 1) error messages - which are so serious that the
compiler cannot translate the code and 2) warning messages which mean -
"There might be a problem here that you should look at but I will go ahead
and translate your code.") Watch out for these warning messages!
The compiler can make the translation when you use '=' instead of '=='
in a test condition because all operators in C++ return a valueand
because of the way C++ treats True and False. "Returning a value" isan
idea that you have already implicitly worked with. When you write: the '*' operator 'returns' the value calculated by multiplying
'hoursWorked'by 'rateOfPay'. If '*' did not 'return' a value, there would
be nothingto store in 'fullSalary'. All arithmetic operators (such as +,
+=, -, -+,*=, /, /+, ++, --) return a value for the same reason.
In C++ not only arithmetic operators but all other types of operators
return a value. For instance, the relational operators return a value.But,
what value do '<', '>', '==' or the other relational operators
return? It turns out that they return the value TRUE or FALSE where FALSE
is defined as zero and TRUE is defined as ANY other value - – with 1 being
the usual value. In other words, if we have the strange test "3 < 5",
the'<' operator will return '1' since 3 is less than 5. But, if we have
"3 == 5", the operator '==' returns 0 since 3 does NOT equal five. (Bythe
way, note that "3 == 5" and "3 < 5" are not statements of fact asthey
would be in mathematics, they are operators that perform a test -Remember
our earlier discussion of the difference in the way mathematicsand C++
interpret the '=' symbol.)
The final piece to this puzzle is that the assignment (=) operator
returnswhatever value has been assigned. In other words, if we have the
line ofcode: 'sum' gets the value 7 and 7 is returned by the '=' operator.
Now considerthe line: The programmer probably meant to write "(sum == 0)" which simply tests
to see if 'sum' contains 0. However, by writing "(sum = 0)" the programmer
is telling the computer to place 0 in the memory location symbolized bythe
variable 'sum' and return that 0. Two problems arise: First, sincethe code
always returns 0, the test is always determined to be False (sinceFalse is
defined in C++ as 0)! Second, whatever value was in the variable'sum' has
been lost, replaced by 0. It is possible but unlikely that thisis what the
programmer wanted. The moral of the story - "Be careful touse "==' instead
of '=' in equality tests." and "Pay attention to warningmessages." (OK, so
there are two morals to this 'story'.)
With this we complete our first look at the basics of C++. In the next
chapter we will begin exploring tools we can use to make sure that thecode
we write is not only syntactically (grammatically) correct but thatit
causes a computer to perform the steps we actually want performed.
|
|