Turtlegraphics for Smartphones and Tablets

Bern University of Teacher Education  
HomeStart Online EditorPrintAndroid GamesJava-Online

if - else - structure


Program branches on the basis of certain conditions belong to the basis structure of every program language. The instructions following if are only conducted it the condition is fullfilled. Othewise the instructions following else will be conducted.

In the first example 20 steps are drawn. Each uneven steps shall be drawn in red, each even step in green. If the number i is even or uneven can be proven with the modulo division by 2. If the result of the modulo division is 0 (i % 2 == 0), the number is even, otherwise it is uneven.

Edit source with online-compile

Install App on smartphone or tablet

QR-Code

Download sources (Tu6.zip)

// Tu6.java

package app.tu6;

import turtle.*;

public class Tu6 extends Playground
{
  void main()
  {
    setPos(-195-195);
    setPenWidth(3);
    for (int = 0; i < 13; i++)
    {
      if (i % == 0)
        setPenColor(RED);
      else
        setPenColor(GREEN);      
      fd(30).rt(90).fd(30).lt(90);
    }
  }
}
 

Explanatations to the program code:

if (i % 2 == 0)

Additionally to the calculating operation +, -, * and / when programming, mostly the so called Modulo-Division % is used. This gives the rest out of the whole number division.
Conditions are indicated with the aid of comparing operations:
>, >= , < , <= , == , !=
Attention: equality operator == is always written with two equality signs

setPos(-195, -195)

The turtle is positioned in the corner down left

setPenWidth(3)

The turtle draws 3 Pixel thick lines



example 2: line graphic with two colours

Edit source with online-compile

Install App on smartphone or tablet

QR-Code

Download sources (Tu6a.zip)

// Tu6a.java

package app.tu6a;

import turtle.*;

public class Tu6a extends Playground
{
  void main()
  {
    for (int = -200; i <= 200; i = + 10)
    {
      if (i < 0)
        setPenColor(RED);
      else
        setPenColor(GREEN);
      
      setPos(0, 200);
      moveTo(i, -200);
    }
  }
}
 

Explanations to the program code:

for (int i = -200; i <= 200; i = i + 10)

i = i + 10: after each loop cycle i is aumented by 10 j
In short you may also just write i += 10

setPos(0, 200)

The turtle starts drawing each line on the point (0, 200)

moveTo(i, -200)

Moves the turtle from the current position to the point with the coordinates (i, -200), whereat i can accept values (-200, -190. -180.... 200).



Multiple selection


Also multiple conditions can be proven consecutively. In our example, initially the pen colour is red, then green and for the last square yellow. The method fill() fills with the pen colour the closed area in which the turtle is.

Edit source with online-compile

Install App on smartphone or tablet

QR-Code

Download sources (Tu7.zip)

// Tu7.java

package app.tu7;

import turtle.*;

public class Tu7 extends Playground
{
  void main()
  {
    setPos(-195-195);
    for (int = 0; i < 13; i++)
    {
      if (i < 4)
        setPenColor(RED);
      else
        if (i < 8)
          setPenColor(GREEN);
        else
          setPenColor(YELLOW);

      for (int = 0; k < 6; k++)
        fd(30).rt(90);
      rt(45).fd(4).fill();
      bk(4).rt(135);
    }
  }
}
 


Explanations to the program code:

rt(45).fd(4).fill()

In order to fill the squares successively, the turtle is moving under an angle of 45° in a short trace, colours the square and then moves back to the starting point.