Turtlegraphics with Java
HomeAufgabenDruckenJava-Online

while-structure (Iteration)

To draw a square with the Turtle in the first example Tu1, the programer had to repeat the two lines
joe.forward (100);
joe.right (90);

four times.
A more elegant solution is the Java program structure of the Iteration (Repetition). One or more condotions are set and the following actions are executed as long as the conditions are meet.


// Tu2.java

import ch.aplu.turtle.*;

public class  Tu2
{
  Turtle joe = new Turtle();

  public Tu2()
  {
    int = 0;            
    while (i < 4)         
    {
      joe.forward(100);   
      joe.right(90);
      i++;                
    }
  }

  public static void main(String[] args)
  {
    new Tu2();
  }
}
 

 

Explaining the program code:
i++
is equal to i = i + 1. The value of i is increased by 1 after each repetition.