Turtle Graphics with Java

Bern University of Teacher Education  
HomeStart Online-EditorPrintAndroid-TurtlegraphicsJava-Online

if-else-structure (Selection)

Program branching based on certain conditions is part of the default structures of each programming language. The actions after if are only executed if the conditions are meet. Otherwise the actions after else are executed.

Run this example (Tu5.java)

//Tu5.java
import ch.aplu.turtle.*;

class Tu5
{
  Turtle joe = new Turtle();

  Tu5()
  {
    for (int i = 0; i < 50; i++)
    {
      double a = Math.random();
      if (> 0.5)
        joe.right(90);
      else
        joe.left(90);

      joe.forward(20);
    }
  }

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

Test this example in the Online-Editor

Explaining the program code:
double  a  = Math . random () ;
The method Math.random () generates a random multi-digit decimal number between 0 and 1 (e.g. 0.48245923)

if ( a  > 0 . 5 )

The conditions are defined by logical operators (boolean operators):
>, >= , < , <= , == , !=


if-structure (Unilateral selection)

Often only one condition has to be meet. In this case the else-part of the selection can be left out. The program branches only into the if-section if the conditions are meet.

Run this example (Tu6.java)

//Tu6.java
import ch.aplu.turtle.*;

class Tu6
{
  Turtle joe = new Turtle();

  Tu6()
  {
    for (int i = 0; i < 12; i++)
    {
      joe.penUp();

      if (% 2 == 0)
        joe.penDown();

      joe.forward(15);
    }
  }

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

Test this example in the Online-Editor


Explaining the program code:
i % 2

Besides the default arithmetic operations +, -, * and / the so called modulo division % can be used. It returns the remainder of a division.
The modulo operation is often used to check if a number is even or odd e.g. i % 2 returns 0 if i is an even number or 1 if it is odd.

if (i % 2 == 0)

The conditions are defined by logical operators (boolean operators):
>, >= , < , <= , == , !=
Be sure that the logical operator == is always written with two equal signs.
joe. penUp () The Turtle does not draw, but still moves according to the program statements.
joe. penDown () The Turtle draws again.

 

if-else - if-else-structure (Multiple selection)

It is also possible to have several selections after one another. To be able to read the program code easily it is important to clearly structure it. For example the actions after an if-condition is slightly indented. E.g:
if (i < 50)
  joe.forward(2);

Run this example (Tu7.java)

//Tu7.java
import ch.aplu.turtle.*;

class Tu7
{
  Turtle joe = new Turtle();

  Tu7()
  {
    for (int i = 0; i < 180; i++)
    {
      if (< 50)
        joe.forward(2);
      else
        if (< 100)
        {
          joe.right(3.6);
          joe.forward(2);
        }
        else
          if (< 150)
          {
            joe.hideTurtle();
            joe.left(3.6);
            joe.forward(2);
          }
          else
            joe.forward(20);
    }
  }

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

Test this example in the Online-Editor


Explaining the program code:
joe.hideTurtle() The Turtle becomes invisible. This enables it to move and draw faster

Exercises