Connecting Tech Pros Worldwide Forums | Help | Site Map

Repetition or Iteration

Expert
 
Join Date: Nov 2006
Location: UK
Posts: 1,320
#1   Apr 11 '07
Repetition or Iteration

The third control structure allows us to repeat parts of a program. The first form is
Expand|Select|Wrap|Line Numbers
  1.   while (some condition) do something
Where the condition is written using comparison operators.

e.g. print a line of *
Expand|Select|Wrap|Line Numbers
  1.  
  2.   public class Line
  3.      {
  4.      public static void main(String argv[])
  5.        {
  6.        int i=5;
  7.        while (i>0)
  8.          {
  9.           System.out.print('*');
  10.           i--;
  11.          }
  12.        System.out.println();
  13.        }
  14.      }
  15.  
A loop may contain any code required, including another loop with each level of nesting is indented further so that the structure is immediately visible.

e.g. A nested loop that will print the pattern
****
***
**
*
Expand|Select|Wrap|Line Numbers
  1.     int linecount = 4;
  2.     while (linecount>0)
  3.     {
  4.       int starcount = linecount;
  5.       while (starcount>0)
  6.       {
  7.         System.out.print("*");
  8.         starcount--;
  9.       }
  10.       System.out.println();
  11.       linecount--;
  12.     }
  13.  
A loop has several distinct parts: initialisation (linecount = 4), condition (linecount>0), loop body and 'housekeeping' (linecount--).

A shorthand form exists: the for loop
Expand|Select|Wrap|Line Numbers
  1.   for(initialisation;condition;housekeeping)
(1) Initialisation is done before the loop starts, then the condition is tested.
(2) Following that the loop body is executed and then the housekeeping is done, followed by the test again.

e.g. to display a block of *'s
Expand|Select|Wrap|Line Numbers
  1.  
  2.   public class Block1
  3.     {
  4.      public static void main(String argv[])
  5.        {
  6.         for (int i=5;i>0;i--)
  7.           {
  8.            for (int j=4;j>0;j--)
  9.                System.out.print('*');
  10.            System.out.println();
  11.           }
  12.        }
  13.     }
  14.  
The do loop
The condition is tested at the end of the loop: a useful structure for the times when a loop body must always be done at least once.

For example, the value epsilon is specified thus:
Expand|Select|Wrap|Line Numbers
  1.     1.0 + epsilon != 1.0
  2.  
The value of epsilon is the smallest number which, when added to the value 1.0, changes the value, i.e. adding any smaller value to 1.0 still gives 1.0. A pseudo-code version of the algorithm often used to calculate the values of epsilon is:
Expand|Select|Wrap|Line Numbers
  1.     epsilon = 1.0
  2.     do
  3.         epsilon = epsilon / 2.0
  4.     while ( 1.0 + epsilon != 1.0 )
  5.     epsilon = 2.0 * epsilon
  6.  
which can be implemented in Java using a do loop
Expand|Select|Wrap|Line Numbers
  1. public class epsilon
  2. {
  3.   public static void main(String s[])
  4.    {
  5.     float epsilon = 1.0f;
  6.  
  7.     do
  8.         epsilon = epsilon / 2.0f; 
  9.     while ((1.0f + epsilon) != 1.0f);
  10.     System.out.println("  float epsilon calculated  = " + epsilon * 2.0f);
  11.    }
  12. }



Closed Thread