For and While

Overview

In computer programming, a loop is a statement that continually iterates a sequence of codes until a certain condition is reached. There are two types of loops in Mobius, For and While. As variables defined in a loop cannot be directly referenced outside of a loop, we will have to create lists to extract values out of the loops.

Loops are key procedures that bring out the importance of computational thinking in efficient 3D modelling. The efficiency is further improved in cases where multiple loops are nested within one another.

While loops may seem rather challenging to apply at first, users are encouraged to constantly experiment with them, for the mastery in utilising the loops will prove to be extremely beneficial. After having a good grasp of the loops, users can create complex models using loops as shortcuts. This will quicken model making processes, especially when contrasted against direct modelling methods.

See Extract data out of a Loop Using Lists


For

The For statement runs every element in a finite list through the same sequence of codes. As such, it is also known as a For loop. In other words, it can be understood as for each.


Application of For

The For function can operate on elements in any lists, be it a list of numbers or a list of entities.


Example 1. For item in a list of entities

Each pgon in a list of pgons are cut with a copied and scaled down version of itself.

Application of For 1


Example 2. For item in a list of numbers

The list of number, range(0, 10) is used to generate 10 copies of pgon that are equally spaced out, as each of them is moved a distance ranging from 0 unit to 45 units from pgon.

Application of For 2


While

The While statement iterates a sequence of codes as long as the specified conditions remains true. It is also known as a While loop.


Application of While

As long as the total number is less than 100, the number of apples will increase by 4 each round. The process stops right before the total number of apples exceed 100.

Example of a While statement
Figure: Example of a While statement


Loops Within a Loop

Loop(s) can be created within a loop. There is no limit to the number of loops that can be used within one.


Example

To understand a block of For loop like this, learners are encouraged to take time to use the terminate function and print the returns from each statement to understand how the Procedure Lines work with one another in the Procedure Space.

a For Loop within a For Loop
Figure: a For Loop within a For Loop

results matching ""

    No results matching ""