Object-Oriented Programming (From Problem Solving to JAVA) (Charles River Media Programming)

8.5 Repetition with Loop Until

The loop-until construct is similar to the while loop. The main difference is that in the loop-until, the condition is evaluated after the repeat group. This means that the operations in the repeat group, Block1, will be carried out until the condition is true. Figure 8.3 shows a portion of the flowchart for the loop-until construct.

Figure 8.3: Flowchart segment for the loop-until construct.

Note

Independent of the initial evaluation of the condition, the operations in the loop will be carried out at least once. If the condition is true, the operations in the repeat group will be carried out only once.

The KJP code for the loop-until construct is written with the repeat compound statement, which uses the keywords repeat, until, and endrepeat. The repeat group consists of all operations after the repeat keyword and before the until keyword.

The general concepts of loop counters and loop conditions also apply to the loop-until construct. The following portion of code shows the general structure for the loop-until statement.

repeat 〈 statements in Block1 〉 until 〈 condition 〉 endrepeat

As a simple example, consider a mathematical problem of calculating the summation of a series of integer numbers. These start with some given initial number, with the constraint that the total number of values added is below the maximum number of values given; for example, the summation of integer numbers starting with 10 in increments of 5 for a total of 2000 numbers. For this problem, a counter variable, numbers, is used to store how many of these numbers are counted. An accumulator variable is used for storing the value of the intermediate sums. Every time through the loop, the value of the increment is added to the accumulator variable called sum.

Class Sum computes the summation of a series of numbers. The KJP implementation of this class follows.

On the CD

The KJP code that implements class Sum is stored in the file Sum.kpl.

description This class calculates summation for a series of numbers. */ class Sum is public description This is the main function in the program. */ function main is variables integer sum // accumulates summations integer numbers // number of values to sum integer svalue // starting value integer maxnum // maximum number of values integer inc_value // increment value begin // body of function starts display "Enter starting value: " read svalue display "Enter number of values: " read maxnum display "Enter increment: " read inc_value set sum = svalue set numbers = 0 repeat add inc_value to sum increment numbers until numbers >= maxnum endrepeat display "Summation is: ", sum, " for ", numbers, " values" endfun main endclass Sum

On the CD

The code for the Java implementation of class Sum follows. This code is stored in the file Sum.java and was generated by the KJP translator.

// KJP v 1.1 File: Sum.java, Fri Dec 06 14:23:54 2002 /** This class calculates summation for a series of numbers. */ public class Sum { /** This is the main function in the program. */ public static void main(String[] args) { // data declarations int sum; // accumulates summations int numbers; // number of values to sum int svalue; // starting value int maxnum; // maximum number of values int inc_value; // increment value // body of function starts here System.out.println("Enter starting value: "); svalue = Conio.input_Int(); System.out.println("Enter number of values: "); maxnum = Conio.input_Int(); System.out.println("Enter increment: "); inc_value = Conio.input_Int(); sum = svalue; numbers = 0; do { sum += inc_value; numbers++; } while (!( numbers >= maxnum) ); System.out.println("Summation is: "+ sum+ " for "+ numbers+ " values"); } // end main } // end Sum

The execution of this program with the indicated input values is shown in Figure 8.4.

Figure 8.4: Execution of program with class Sum.

Категории