|
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. Working With a Sentinel Value
Study carefully the first line of the "while loop with a priming read"
pattern:
and notice that it says that only the variable(s) involved in the test
condition need be retrieved from the user before the while loop begins. In
the Salary Calculation program we only need one indicator from the user
that he or she is finished and that can either be a specific value for the
rate of pay or for the hours worked. Let's assume that any negative value
for the rate of pay will indicate that there are no more employees. If
this is the case, only the rate of pay needs to be retrieved before the
'while' loop and our algorithm can be modified as
follows:
Writtten as C++, this algorithm is translated as
follows: B. The 'for' loop We saw earlier how to accomplish this for 10 employees with a 'while' loop . There is,
however, a simpler way to do this involving yet another C++ instruction.
In the 'while' loop version (the key parts of which are shown below):
We will explore the tremendous power of this statement later in Chapter 8, Section VII and in Chapter 10. Here, however, is how
we would write a for loop that executed some code ten times:
for (int count = 0; count < NUM_TIMES; count++) Everything of importance for the loop is inside the parenthesis of the
'for' statement. First is the initialization of the 'count' variable, then
there is the test to see if count remains less than NUM_TIMES, and finally
there is the instruction to increment count by 1 each time through the
loop. The first time through the loop 'count' is set to 0 and its value is
tested to make sure it is less than NUM_TIMES. If for some reason we had
set the value of count to 10 or greater, the loop would never have been
entered! Once the system sees that 'count' is less than NUM_TIMES, the
loop is entered and whatever instructions are inside the loop are
executed. At the end of the loop 'count' is automatically incremented by 1
and its value is again compared with NUM_TIMES. Again, if its value is
less than NUM_TIMES the loop is entered, the code inside the loop is
executed and 'count' is incremented. This continues until 'count' is no
longer less than NUM_TIMES. Note: we could have used the value '10' inside
the 'for' statement instead of NUM_TIMES but it is better to use a named
constant whenever possible.
Here is the full code to calculate the salary of ten employees: 'For' loops have a number of uses but the one we just saw - to loop
through a group of instructions some set number of times - is the most
common. Often, the exact number of times is not known when the code is
being written even though it will be known before the loop is executed.
This would happen if, for example, the user was to enter the number of
times the loop is to execute or if there was some way to calculate the
number of times to loop. Here is a general C++ based pattern that captures
the key idea of this notion of getting or calculating the number of times
to loop:
for ( int Counter Variable = 0; Counter Variable < Num
Times; Counter Variable++) Finally, the 'for' statement can be used with the sum, counter and
average patterns. The code for those patterns would remain the same but
there would be changes in the loop code. You might want to review both the
code that used a 'while' loop to process ten employees and the 'while'
loop code that processed some unknown number of employees. Note that the
version involving an unknown number of employees requires a sentinel value
and a priming read while the other version has no priming read. Since the
'for' loop (as we have studied it so far) is used when the loop is to be
executed a pre-determined number of times, there is, similarly, no need
for a priming read.
|