Turtlegraphics with Java
HomeAufgabenDruckenJava-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.


//Tu5.java

import ch.aplu.turtle.*;
import java.awt.Color;

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

  public Tu5()
  {
    joe.setPos(-200-200);
    joe.setLineWidth(5);
    for (int = 0; i < 10; i++)
    {
      if (i % == 0)
        joe.setPenColor(Color.red);
      else
        joe.setPenColor(Color.green);  
      
      joe.fd(40).rt(90).fd(40).lt(90);
    }
  }

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

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.


//Tu6.java

import ch.aplu.turtle.*;
import java.awt.Color;

public class  Tu6
{
  Turtle = new Turtle();

  public Tu6()
  {
    t.addStatusBar(20);
    t.speed(-1);
    t.setPenColor(Color.red);
    for (int = 4; s < 200; s = + 2)
    {
      if (s == 140)
        t.setPenColor(Color.green);
      t.forward(s);
      t.right(90);
      t.setStatusText("Size: " + s);
    }
  }

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

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);


//Tu7.java

import ch.aplu.turtle.*;
import java.awt.Color;

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

  public Tu7()
  {
    joe.hideTurtle();
    joe.clear(Color.black);
    joe.setLineWidth(4);
    for (int = 0; i < 120; i++)
    {
      if (i % == 0)     
        joe.setPenColor(Color.red);       
      else
        if (i % == 1)
          joe.setPenColor(Color.green);
        else
          joe.setPenColor(Color.magenta);
 
      joe.forward(150);
      joe.back(150);
      joe.left(3);  
    }
  }

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

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