Formulating Algorithms: Sentinel-Controlled Repetition

Develop a class-averaging program that processes grades for an arbitrary number of students each time it is run.

Sentinel-controlled repetition is often called indefinite repetition because the number of repetitions is not known before the loop begins executing.

A special value called a sentinel value (also called a signal value, a dummy value or a flag value) can be used to indicate "end of data entry"

A sentinel value must be chosen that cannot be confused with an acceptable input value.


Top-down, stepwise refinement

Begin with a pseudocode representation of the top—a single statement that conveys the overall function of the program:

The top is a complete representation of a program. Rarely conveys sufficient detail from which to write a Java program.

Divide the top into a series of smaller tasks and list these in the order in which they’ll be performed.

First refinement:

This refinement uses only the sequence structure—the steps listed should execute in order, one after the other.

Many programs can be divided logically into three phases:
    an initialization phase that initializes the variables;
    a processing phase that inputs data values and adjusts program variables accordingly;
    a termination phase that calculates and outputs the final results.

Second refinement: commit to specific variables.

The pseudocode statement    Initialize variables    can be refined as follows:

The pseudocode statement    Input, sum and count the quiz grades
requires a repetition structure that successively inputs each grade
We do not know in advance how many grades are to be processed, so we’ll use sentinel-controlled repetition.
The second refinement of the preceding pseudocode statement is then

Prompt the user to enter the first grade
Input the first grade (
possibly the sentinel)
While the user has not yet entered the sentinel
      Add this grade into the running total
      Add one to the grade counter
      Prompt the user to enter the next grade
      Input the next grade (possibly the sentinel)

The pseudocode statement    Calculate and print the class average    can be refined as follows:

If the counter is not equal to zero
      Set the average to the total divided by the counter
      Print the average
else
      Print “No grades were entered”

Test for the possibility of division by zero—a logic error that, if undetected, would cause the program to fail or produce invalid output.

Click here for the codes.

Program logic for sentinel-controlled repetition

In a sentinel-controlled loop, the prompts requesting data should remind the user of the sentinel.


Integer division yields an integer result.

To perform a floating-point calculation with integers, temporarily treat these values as floating-point numbers for use in the calculation.

The unary cast operator (double) creates a temporary floating-point copy of its operand.

Cast operator performs explicit conversion (or type cast).

The value stored in the operand is unchanged.

Java evaluates only arithmetic expressions in which the operands’ types are identical.

Promotion (or implicit conversion) performed on operands.

In an expression containing values of the types int and double, the int values are promoted to double values for use in the expression.

Cast operators are available for any type.

Cast operator formed by placing parentheses around the name of a type.

The operator is a unary operator (i.e., an operator that takes only one operand).

Java also supports unary versions of the plus (+) and minus () operators.

Cast operators associate from right to left; same precedence as other unary operators, such as unary + and unary -.

This precedence is one level higher than that of the multiplicative operators *, / and %.