8.6 Repetition with for Loop


8.6 Repetition with for Loop

The for loop is useful when the number of times that the loop is carried out is known in advance. The for loop explicitly deals with the loop counter. In the for statement, the initial value and the final value of the loop counter has to be indicated. The general structure of the for statement follows. The repeat group consists of the sequence of instructions (written as statements) in Block1.

       for  counter  =  initial_value  to  final_value           do             Block1       endfor 

Every time through the loop, the loop counter is automatically incremented. The last time through the loop, the loop counter has its final value allowed. In other words, when the loop counter reaches its final value, the loop terminates.

The keywords that appear in this statement are: for, to, downto, do, and endfor. The for loop is similar to the while loop in that the condition is evaluated before carrying out the operations in the repeat loop.

As an example of an application with a for statement, consider the problem of finding the maximum number from a list of integer numbers read one by one from the input device. The algorithm for the solution to the problem first reads the number of values to read and from which to compute the maximum.

An intermediate storage variable called maximum is used to hold the maximum value found so far. Its initial value is set to zero, and every time through the loop, this value is compared with the new value read. If the new value read is greater than the maximum, then this new value becomes the new maximum. After the loop terminates, the last value of maximum is the one printed.

Class Max uses the for loop to implement the algorithm with repetition. The following is the code for the KJP implementation of class Max.

       description          This class calculates the maximum of a list of          numbers. This is the only class.  */       class Max is         public         description             This is the main function in the program. */         function main is         variables           real maximum           // maximum so far           real x           integer num_values     // values to read           integer lcounter       // loop counter         begin           set maximum = 0.0           display "Enter number of values to read: "           read num_values           for lcounter = 1 to num_values do               display "Enter value: "               read x               if x greater than maximum then                  set maximum = x               endif           endfor           display "Maximum value found: ", maximum         endfun main       endclass Max 

On the CD

Class Max is stored in the file Max.kpl.

The code for the Java implementation of class Max follows.

       // KJP v 1.1 File: Max.java, Fri Dec 06 15:32:28 2002       /**          This class calculates the maximum of a list of numbers.          This is the only class.  */       public  class Max  {       /**            This is the main function in the program. */         public static void main(String[] args) {           // data declarations           float  maximum;  // maximum so far           float  x;           int  num_values; // number of values to read           int  lcounter;   // loop counter           maximum =  0.0F;           System.out.println(                       "Enter number of values to read: ");           num_values = Conio.input_Int();           for (lcounter = 1 ; lcounter <= num_values;                                             lcounter++) {               System.out.println("Enter value: ");               x = Conio.input_Float();               if ( x > maximum) {                  maximum =  x;               } // endif           } // endfor           System.out.println("Maximum value found: "+                  maximum);         }  // end main       }  // end Max 

On the CD

The Java implementation of class Max is stored in the file Max.java and was generated by the KJP translator.

Figure 8.5 shows the output for the execution of the program with class Max.

click to expand
Figure 8.5: Execution of program with class Max.




Object-Oriented Programming(c) From Problem Solving to Java
Object-Oriented Programming (From Problem Solving to JAVA) (Charles River Media Programming)
ISBN: 1584502878
EAN: 2147483647
Year: 2005
Pages: 184

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net