6. Control Flow

6.1. What is Control Flow?

Within Actimator, commands are executed in sequence from top to bottom. There are two ways to specify the order in which commands should be executed, these are Control Flow Statements.

The two types of Control Flow are Conditionals(If Statements) and Iterations(For Loops).

6.2. Making Decisions

If

The If Statement checks a Condition. If the condition is met, It executes the commands located inside the If Body.

If statement

If you don’t specify a condition for the If Statement, the code inside the If Body will be executed.

Else

In some situations, we want to take action when a condition is not met. For this, press the ELSE button. Else button

Commands contained in the Else Body are executed when a condition is not met.

Else

The program pictured above can be phrased in normal language:

If Player One’s Score is greater than Player Two’s Score, Player One wins!!! Otherwise, Player Two wins!!!

6.3. Iteration

You may need to execute commands repetitively. A For Loop steps in the amount given until a condition is met. Commands inside the loop are executed each step(iteration).

Loop-cycle

A loop can be broken up into these four parts:

Assignment

You assign an initial value to a variable at the start of the loop.

Condition

The loop will continue to step as long as its condition is met. For example, it will step until its variable reaches a certain value.

If the condition is not met, the loop will exit.

Loop Body

The commands in the Loop Body are executed each iteration.

Step

After the Loop Body, the variable will change its value and the loop will return to the condition.

The following program initializes the loop by setting the variable “x” to 0. As long as x is less than 5, the loop will create a Box Entity with a horizontal position of 32 * x. After creating the box, the loop completes its iteration by increasing x by 1. The loop then cycles back to the condition.

For loop

This program results in five boxes being created on the scene. Each one is created 32 pixels to right of the previous box.

Scene loop

Infinite Loops

You can make a loop that continues cycling the entire time the game is running. Loops can be demanding on your device’s processor, so be careful with loops whose condition will always be met.