Java Repetition Structures                 

   Contact
   C
   C++
   Visual Basic
   Java
   JavaScript
   DHTML
   Style Sheets
   About
   Normalization
   Active X
   TDC Binding
   PHP
   Perl and CGI
   Flash
   XML
   SQL
   Chat
   MCSE
   Linux
   Cabling   
 

   
 
    
    
Java 2  Java 3  Supplementary  Events  AWT Project  AWT Project  Language  Swing  Sound/Images


Repetition structures involve looping an serve a vital function within a program. As in C++, there are three basic kinds of loops in Java: the "for" loop, the "while/true" loop and the "do/while" loop.

Example 1 - "for" loop:

import javax.swing.*;

public
class LoopStuff
{
      public void main(String[] args)
      {
             int num = 0;
             int GUESS = 0;
             final int NumGuesses = 3;
             String PlayerGuess = "";

 
              for(int z = 0; z < NumGuesses; z++)
              {

 
                 System.out.println("Guess my number:    ");
                  try { PlayerGuess = cin.readLine(); }
                  catch(IOException e){System.err.println("Error");}
                 
                  GUESS = Integer.parseInt(PlayerGuess);
             
                  if(GUESS < num)
                  {
                      System.out.println(
                      "You guessed lower.");
                  }
 
                  else if(GUESS > num)
                  {
                      System.out.println(
                      "You guessed higher.");
                  }

                  else if(GUESS == num)
                  {
                      System.out.println("You guessed it!.");
                      break;                
                  }

              }


             System.exit(0);

      }
//close main() function
 
}
//close LoopStuff class

Notice: The for loop above would execute 3 times, giving the user 3 guesses. It is contained within the body:
"for
(z = 0; z < NumGuesses; z++)"
, where the keyword "for" defines it as a for loop. The are three basic parts to any for loop: "z = 0" initializes it to the starting value, "z < NumGuesses" is the test condition evaluated as true or false which will become the sentinel value to break the loop, and "z++" is the action that is performed each time the loop iterates.

Example 2 - "while/true" loop:

import javax.swing.*;
import java.io.*;

public
class LoopStuff
{
      public void main(String[] args)
      {
             int z = 0;
             final int NumGuesses = 3;

             while(z < NumGuesses)
              {

 
                 System.out.println("Guess my number:    ");
                  try { PlayerGuess = cin.readLine(); }
                  catch(IOException e){System.err.println("Error");}
                 
                  GUESS = Integer.parseInt(PlayerGuess);
             
                  if(GUESS < num)
                  {
                      System.out.println(
                      "You guessed lower.");
                  }
 
                  else if(GUESS > num)
                  {
                      System.out.println(
                      "You guessed higher.");
                  }

                  else if(GUESS == num)
                  {
                      System.out.println("You guessed it!.");
                      break;                
                  }


                  z++;
              }


             System.exit(0);

      }
//close main() function
 
}
//close LoopStuff class

Notice: The above example does the same thing as the previous for loop, but this time it is using a while/true loop. You can see that the action performed is within the block in this kind of loop. In console programming, while/true loops are often used to lock the user into a specific function until an appropriate value has been received.


Example 3 - "do/while" loop:

public class LoopStuff
{
      public void main(String[] args)
      {
             int z = 0;
             final int NumGuesses = 3;

              do
              {
 
                 System.out.println("Guess my number:    ");
                  try { PlayerGuess = cin.readLine(); }
                  catch(IOException e){System.err.println("Error");}
                 
                  GUESS = Integer.parseInt(PlayerGuess);
             
                  if(GUESS < num)
                  {
                      System.out.println(
                      "You guessed lower.");
                  }
 
                  else if(GUESS > num)
                  {
                      System.out.println(
                      "You guessed higher.");
                  }

                  else if(GUESS == num)
                  {
                      System.out.println("You guessed it!.");
                      break;                
                  }


                  z++;
              }
while(z < NumGuesses)

             System.exit(0);

      }
//close main() function
 
}
//close LoopStuff class


Notice: The above do/while loop is similar to the while/true loop in terms of what it does. The difference is that it will perform the actions defend within the block of code at least once before evaluating the expression. With a while/true loop, if the condition being tested for is false, the contents of the loop may never be executed. With do/while, you can be sure the contents of the loop will execute at least once when that is needed. As a result, a while/true loop is called a "pre-test" loop and a a do/while loop is called a "post-test".

Java 2  Java 3  Supplementary  Events  AWT Project  HOME  AWT Project  Language  Swing  Sound/Images