Chapter 2: Program Flow

The Flowchart

In order to solve difficult tasks in programming, it is often helpful to outline the program flow in a flowchart. The following symbols are used in flowcharts:

Start / Stop
Decision / Condition

TASK 2.01

Complete the flowchart so that Kara reaches the leaf in all worlds with the following properties:

Task 1 - World 1

Task 1 - World 2

  • The leaf is always right in front of Kara - Kara only needs to walk around the trees.
  • There are never two trees standing side by side.

Task 1 - Flowchart

TASK 2.02

Draw an extended diagram so that Kara picks up the leaf at the end if he finds it. Here is an overview of all available methods of Kara as a help:

Kara Methods

Conditional Statements in Java

if (treeFront()) {      // Condition.
	turnLeft();         // Block 1, executed if the condition is true.
} else {
	move();             // Block 2, executed if the condition is false.

Note: The else part (block 2) may be omitted if it is not needed.

TASK 2.03

  1. Describe with words what the following code does.
  2. Sketch it as a flowchart.
if (onLeaf()) {
} else {

TASK 2.04

  1. Describe with words what the following code does.
  2. Sketch it as a flowchart.
if (onLeaf()) {

TASK 2.05

Conditional statements can be nested.

  1. Describe what happens when you run the program.
  2. Draw a corresponding flowchart.
if (treeLeft()) {
	if (onLeaf()) {
	} else {
} else {


TASK 2.06: Around Tree II

  • Open the scenario Kara 206… from the folder scenarios-chapter-2. In this scenario, the method goAroundTree() is already programmed and part of the act() method is prepared.
  • Program inside the act() method what you have drawn in Task 2 as a flowchart.
  • In this scenario, you have several worlds (a, b and c).
  • Your program should work in any of those worlds without error messages.

TASK 2.07: Nested Conditions

  • Open the scenario Kara 207… in Greenfoot and write the program as outlined in task Task 5.
  • Modify the program so that Kara only picks up the leaf if no tree is on his side.

Logical Operations

Our Kara can already do more than just execute simple commands. Kara will react differently based on a test. It should also be possible for Kara to react simultaneously to two or more tests.

The following table shows the three main logical operators in Java:

Operator Description Example
&& and treeFront() && onLeaf() Is only satisfied (true) if both statements are true, i.e. if Kara is facing a tree and is on a leaf.
|| or treeFront() || onLeaf() Is satisfied (true) if either one or the other or both statements are true.
! not !treeFront() Changes an expression of true to false and vice versa. This statement would be satisfied (true) if Kara is not facing a tree.

An example in Java would look like this:

if (treeLeft() && onLeaf()) {
	// Do something ...

or combined:

if (treeLeft() && !treeRight()) {
	// Do something ...

TASK 2.08: Afraid of Tunnel

Task 8

Kara is a little afraid of tunnels. Kara should check on every field whether it is a tunnel entrance (i.e. whether it has trees on both sides). If so, Kara instantly drops a leaf because of the shock.

Load the scenario Kara 208…, write the program and test it with all three worlds.

TASK 2.09: Leaf at Tree

Task 9

Now let Kara go straight and put a leaf anywhere where there is a tree on its left or right or on both sides.

Load the scenario Kara 209… and write the program.

TASK 2.10: Put Leaf Track

Task 10

Kara is running straight ahead and lays a leaf anywhere where there is none. When he reaches the tree he will do nothing (even if the Act-button is pressed again).

Load the scenario Kara 210…, write the program and test it.

TASK 2.11: Round Trip

Kara must find the leaf on this round trip. Each field in the tour has exactly two empty neighboring fields. One empty field always lays behind Kara which is the field he came from.

Load the scenario Kara 211… and write a program for it. Test your program in all three worlds.

Tip: Imagine what must be done each time the Act button is pressed. You can draw a flowchart as a help to find the solution.

TASK 2.12 (difficult): Kara Plays Pacman

Task 12

Kara plays Pacman: Kara is on the first of a long trail of leaves, ending in front of a tree. Kara picks up all leaves and stops in front of the trees.

As a better overview, write some parts of the program in their own methods.


Kara can now react to rules set by us in different situations. But Kara is not yet capable of repeating a specified set of instructions. To execute an instruction block multiple times, loops are used.

As an example, we want to do the following:

Kara moves forward until he hits a tree.

Loop Example

In the flow chart you can see that move() is executed repeatedly, as long as no tree stands in front of Kara.

Loop Example - Flowchart

And this is the notation in Java:

while (!treeFront()) {

TASK 2.13

Task 13

Kara stands in front of a tunnel.

Describe what each of the following loops does, and how many steps Kara takes.

# Code Description Steps
while (treeLeft()) {
Move as long as ther is a tree on the left. 4
while (treeRight()) {
? ?
while (treeLeft() || treeRight()) {
? ?
if (treeLeft()) {
} while (treeLeft() && treeRight()) {
? ?
while (!treeFront) {
  if (treeLeft()) {
? ?

TASK 2.14: Around Tree III

Task 14

This is a similar exercise as in task 6: Kara must find a leaf that lies ahead of him. But now there can be any number of trees in a row.

  • Load the scenario Kara 214… and improve the method goAroundTree() so that Kara can walk around several trees. Test your program in all available worlds.
  • Modify act() so that you can just press the act-button once. Kara should then automatically run around the trees until he reached the leaf. In the end he should eat it again.

TASK 2.15: Climbing Up

Task 15

Kara shall climb arbitrarily long stairs.

Write a method oneStepUp() to make Kara climb a single step. You need to figure out how Kara knows if he still has to climb a step or if he’s reached the top.

Note: The Solution should work with pressing on the Act-button only once.

TASK 2.16 (difficult): Kara as Guard

Task 16

Kara wants to guard the forest. He is endlessly walking along outside the forest.

To help yourself, you can draw a flowchart.

Note: For an infinite loop, we can press the Run-button.

Credits: Ideas and concepts of Kara were developed by Jürg Nievergelt, Werner Hartmann, Raimond Reichert et al. Some Kara exercises are based on material by Horst Gierhardt.

What’s Next?

Continue with Chapter 3: Variables