Loops

These puzzles provide means for specifying iterations in your Puzzles scenario.

Visual programming blocks for loops

Contents

Infinite Loops Caveat

Be careful with these puzzles as it is easy to create infinite loops and hang your browser. If you happened to save such a wrongful scenario, you can restore a previous version of your puzzles from backup.

Puzzles Reference

repeat

This puzzle runs the puzzles placed in the do slot the specified number of times.

Visual programming block to create repeats

For example, the following set of puzzles creates several copies of the cube and places them randomly on the scene.

Example on using repeat block

for each in list

This puzzle iterates through a specified list and runs the puzzles placed in the do slot for each item in that list. The items for corresponding iteration are accesible via an automatically created variable.

Visual programming block to create for-each loops

For example, the following set of puzzles adds annotations to all objects from the list.

For-each block usage example

for each in dict

This puzzle iterates through a specified dictionary and runs the puzzles placed in the do slot for each item in that dictionary. The items for corresponding iteration are accesible via an automatically created key/value variables.

Visual programming block to create for-each loops for dictionaries

For example, the following set of puzzles prints flight directions stored in the dictionary to the browser console.

For-each in dict block usage example

count with

This puzzle is similar to for each, except it gives the loop variable values in a numeric sequence.

Visual programming block to create counters

For example, the following set of puzzles counts from 2 to 10 with the step 2, thus creating numbers 2, 4, 6, 8 and 10, clones the cube on each step, and uses those numbers to position the copies along X axis.

Example script on using counter blocks

repeat while / until

The repeat while puzzle runs the puzzles placed in the do slot as long as a specified logical condition is true. On the contrary, the repeat until puzzle stops running just after a specified logical condition evaluates to false.

Visual programming block to create while and until loops

This puzzle is well suited for situations when something is changing inside the loop and this affects your decision to continue or stop performing the iterations. For example, the following set of puzzles, which uses the repeat while variant, keeps asking for the user's answer as long as the user answers "yes".

Example of while loop

You can use the repeat until variant to ask the same question but in a slightly more annoying manner, making the answer "no" the only option to stop this.

Example of until loop

break / continue

Most loops run until the terminating condition (in the case of repeat puzzles) is met or until all values have been taken by the loop variable (in the case of count with and for each puzzles). This puzzle provide additional means for controlling loop behavior. Although the below examples use the for each puzzle, they can be used with any type of loop.

Break/continue visual logic operators

The break variant provides an early exit. For example, the following set of puzzles performs search for an object named "Cube". Once it is found and the result variable is set, there is no need to iterate any further, and as such we break out of the loop.

Example on using break visual programming operator

On the other hand, the continue variant skips the current iteration so that the next iteration of the loop begins. For example, the following set of puzzles adds annotations to all objects on the scene except "Cube".

Example on using continue visual programming operator

Having Troubles with Puzzles?

Seek help on the forums!