Turtlegraphics for Smartphones and Tablets

Bern University of Teacher Education  
HomeStart Online EditorPrintAndroid GamesJava-Online

while - loop (Iteration)


Repeated conductions of a row of instructions (Iteration) belongs to the basis structures of every program language. The while-loop is introduced with the key word while, follow by a loop condition. The instructions of the loop-block are repeated as long as the condition is fulfilled. If the loop-block consists of only one instruction, the curly brackets may be omitted.

In most programs, place holder for numbers, here concretely for whole numbers, are needed. We are talking about a variable of the type Integer.

Edit source with online-compiler

Install App on smartphone or tablet

QR-Code

download sources (Tu2.zip)

// Tu2.java

package app.tu2;

import turtle.*;

public class Tu2 extends Playground
{
  public void main()
  {
    st();
    int = 0;
    while (i < 4)
    {
      fd(100);
      rt(90);
      i++;
    }
  }
}
 

 

Explanations to the program code:
int i = 0
The variable i is a whole number with the starting value 0
i++
On a par with i = i + 1 . With each loop run i will be augmented by 1

 


Endless while-loop
By using a while (true) -loop, a program block can be repeated ceaselessly. The loop block is repeated until the program is interrupted by click the Home or Back key. Therefore the turtle can be animated during the entire program duration.

Edit source with online-compiler

Install App on smartphone or tablet

QR-Code

download sources (Tu2a.zip)

// Tu2a.java

package app.tu2a;

import turtle.*;

public class Tu2a extends Playground
{
  public void main()
  {
    st();
    while (true)
    {
      fd(3);
      rt(2);  
    }
  }
}
 

 

Explanations to the program code:
while (true)
As long as the program is not being interrupted, the loop conditions are fulfilled