Hello Dart: Introduction to Programming

Part 2: Loops

In the last part, we learned how we can give our player a set of instructions. Instead of listing each and every statement, we can also repeat instructions. In programming such repetitions are called loops.

As a first example of a loop, we will do the following:

TASK 2.01: Loop

Loop

The player should move forward until he can’t move any more.

In Dart we can express this as follows:

while (canMove()) {
  move();
}

The while loop is repeated as long as the condition in the paranthesis returns true. Once the player arrives at the edge, the sensor function canMove() will return false and the player stops moving.

Test the program by adding the above while loop to the start() function of scenario2.01.

Logical Operators

Our sensor functions (see Introduction) all respond with either true or false when you call them. A data type that can either be true or false is called a Boolean.

Boolean values can also be combined or changed by means of logical operators. The following table shows the three most important logical operators in Dart:

Operator Description Example
&& and treeFront() && onStar() Is true if both expressions are true, that means if the player is facing a tree and is on a star.
|| or treeFront() || onStar() Is true if either the first or the second expression, or both are true.
! not !treeFront() Changes the expression from true to false and vice versa. This expression would return true if the player is not facing a tree.

An example in Dart could look like this:

if (treeLeft() && onStar()) {
  // Do something ...
}

or combined:

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

TASK 2.02: Loop Star

Loop Star

Open the scenario 2.02 and program the player with a loop so that he runs forward until he stands on the star.

TASK 2.03: Leaving the Tunnel

Leaving the Tunnel

Our player is in a tunnel and wants to get out. Write a program for scenario2.03 where the player walks out of the tunnel and stops as soon as there is no tree on one of the sides.

At the end he should also put down a star.

TASK 2.04: Afraid of Tunnel

Afraid of Tunnel

The player is afraid of tunnels. He should look on every field if it is a tunnel entrance (that is, whether it has trees on both sides). If this is the case, then he should stop and say that he is afraid in a speech bubble.

TASK 2.05: Climbing Up

Climbing Up

The player must climb arbitraily long stairs.

Write a function called oneStepUp() to make the player climb a single step. You’ll need to figure out how the player knows if he still has to climb a step or if he’s arrived at the top.

For Loops

In addition to the while loop there is an other very important type of loop, the for loop.

for (var i = 0; i < 4; i++) {
  move();
}

In this example, a counter i is used. The counter is first set to 0. At every loop cycle the program checks if i is smaller than 4 and increments i by one. This means that the move() statement is executed four times in this loop.

You will encounter the for loop a lot, in various forms. For the moment it is enough for us to know that it exists.

What’s next?

→ Continue with Part 3: Conditionals


Credits
Planet Cute images by Daniel Cook (Lostgarden.com), published under CC BY 3.0.
Oleg Yadrov improved the “Planet Cute” images and was so kind to let me use them. The images were optimized with the great TexturePacker.
Some exercises in Hello Dartwere inspired by Kara. Kara was developed by Jürg Nievergelt, Werner Hartmann, Raimond Reichert et. al.