# Loops

## Intro to Loops

A loop is a sequence of blocks which is specified once but which may be executed several times in succession. These structures are called loops since the blocks inside the loop (the *body* of the loop) is repeated multiple times, reminiscent of a rope containing loops. Each pass through the loop is called an iteration.

{% hint style="info" %}
For more information on Loops, read this [Wikipedia page](https://en.wikipedia.org/wiki/Control_flow#Loops).
{% endhint %}

## The Loops Blocks

There are four kinds of *loops* blocks you can use in the Dynamic Engine, as well as a special *loop termination* block that allows you to terminate (aka short circuit) a loop if a certain condition is met. The four *loops* blocks are:

1. Repeat N Times
2. Repeat While/Until
3. Count With
4. For Each in List

The loop termination block is: Break Out or Continue With Next Iteration

### Loop: Repeat N times

The simplest repeat block runs the code in its body the specified number of times. For example, the following block will log "Hello!" ten times.

<figure><img src="https://211323355-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDfKsc0R4AcYm9kZgIT2X%2Fuploads%2FbFiSfuQK7RjSIIR8zAnd%2FScreenshot%202024-12-06%20at%201.42.33%E2%80%AFPM.png?alt=media&#x26;token=fc892667-7f0f-4b8a-933f-4007c71511ad" alt=""><figcaption></figcaption></figure>

### Loop: Repeat

The options for the **repeat** dropdown do the following:

* **While** - repeats *while* the condition is *true*
* **Until** - repeats *until* the condition is *true*

#### **Repeat While:**

The *Repeat While* block repeats its body while some condition is *true*.  To determine whether a condition is *true* or *false*, [*Logic Blocks*](https://examind.gitbook.io/v1/build/question-builder/dynamic-questions/dynamic-engine/logic) need to be used. In the following example, the *Repeat While* block is used to assign 3 random numbers between 1 and 100 to a variable named S*orted List*, but continue doing that while those random numbers are not sorted in ascending order.

<figure><img src="https://211323355-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDfKsc0R4AcYm9kZgIT2X%2Fuploads%2FHY6U8kHIEs37PAM0mX44%2FScreenshot%202024-12-17%20at%208.15.49%E2%80%AFAM.png?alt=media&#x26;token=998ff286-f795-4361-8d96-97d6437c280b" alt=""><figcaption></figcaption></figure>

In the example above, it's uncertain how many times the body will be executed. It may execute once or hundreds of times, depending on when the while condition becomes *false*.

{% hint style="info" %}
For more information on Logic Blocks, see the [Logic Blocks Page](https://examind.gitbook.io/v1/build/question-builder/dynamic-questions/dynamic-engine/logic).
{% endhint %}

#### **Repeat Until:**

The *Repeat Until* block is very similar to the *Repeat While* block, except the condition is reversed. *Repeat While* loops repeat their bodies *while* some condition is true, while *Repeat Until* loops repeat their bodies *until* some condition is true. The following blocks are equivalent to the previous example because the loop continues until the sorted list variable is sorted in ascending order.

<figure><img src="https://211323355-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDfKsc0R4AcYm9kZgIT2X%2Fuploads%2FzSkzlWeSn7hMTOFtLgWM%2FScreenshot%202024-12-06%20at%201.51.32%E2%80%AFPM.png?alt=media&#x26;token=a2a54116-9251-4d79-9358-7cf28dda7a8f" alt=""><figcaption></figcaption></figure>

### Loop: Count With

The *Count With* block advances a variable from the first value to the second value by the increment amount (third value), running the body once for each value. For example, the following *Count With* block starts by assigning the value *1* to the variable *i* , then *2*, then *3*, and so on until it reaches *10*. This block will log values of *i* from 1 to 10 in ascending order.

<figure><img src="https://211323355-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDfKsc0R4AcYm9kZgIT2X%2Fuploads%2FEa1MPapNo6iN0WYkeGND%2FScreenshot%202024-12-17%20at%208.22.40%E2%80%AFAM.png?alt=media&#x26;token=89661b0a-4e13-4697-919d-1f91ea7b5eb7" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
Note that *Count With* is called a [For Loop](https://en.wikipedia.org/wiki/For_loop) in most programming languages
{% endhint %}

### Loop: For Each in List

The *For Each in List* block is similar to the *Count With* block, except instead of giving the loop variable values in a numeric sequence, it uses the values from a list in turn. The following *For Each* block logs each *color* in the *colors* list: "red", "green", "blue".

<figure><img src="https://211323355-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDfKsc0R4AcYm9kZgIT2X%2Fuploads%2Fcj8kjMGaRkZXemDHjxeF%2FScreenshot%202024-12-06%20at%201.58.29%E2%80%AFPM.png?alt=media&#x26;token=4fe098ab-82c2-4e15-9372-e98e48526793" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
For more information on For Each, read this [Wikipedia Page](https://en.wikipedia.org/wiki/Foreach)
{% endhint %}

### Loop: Break Out or Continue With Next Iteration

The *Break Out or Continue With Next Iteration* blocks differ from other loops blocks, as they are loop termination blocks. They are meant to be used in conjunction with the other loops blocks.

#### **Break Out Block:**

The *Break Out* block provides an [early exit from a loop](https://en.wikipedia.org/wiki/Control_flow#Early_exit_from_loops). The following example logs "red" on the first iteration and breaks out of the loop on the second iteration when the loop variable is equal to "green". The third item in the list is never reached.

<figure><img src="https://211323355-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDfKsc0R4AcYm9kZgIT2X%2Fuploads%2FPwASzezQu3CkpQX1Ds9e%2FScreenshot%202024-12-06%20at%202.00.08%E2%80%AFPM.png?alt=media&#x26;token=de118ba7-baf6-4385-bba2-1cdac118baf6" alt=""><figcaption></figcaption></figure>

#### **Continue With Next Iteration Block:**

The *Continue With Next Iteration* block causes the remaining code in the body to be skipped and for the next iteration of the loop to begin. The following example logs "red" on the first iteration of the loop. On the second iteration, the *Continue With Next Iteration* block is run, skipping the logging of "green". On the final iteration, "blue" is logged.

<figure><img src="https://211323355-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDfKsc0R4AcYm9kZgIT2X%2Fuploads%2FLRb538PZNN7LkL92hCYf%2FScreenshot%202024-12-06%20at%202.01.07%E2%80%AFPM.png?alt=media&#x26;token=04b492ec-2a37-4739-9f9b-98aa78770b96" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
Note that *Continue With Next Iteration* is called [continue](https://en.wikipedia.org/wiki/Control_flow#Continuation_with_next_iteration) in most programming languages
{% endhint %}

***

## Example Question

Consider the following Fill-in-the-blank question in EXAMIND:

<figure><img src="https://211323355-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDfKsc0R4AcYm9kZgIT2X%2Fuploads%2FscYLtUVK6iB8x2rUnnPs%2FScreenshot%202024-12-06%20at%202.52.08%E2%80%AFPM.png?alt=media&#x26;token=326481e6-2ff0-4206-ba0b-0cc9ffee0c4d" alt=""><figcaption></figcaption></figure>

The goal of this question is to create a simple question that doesn't have negative net income. Using a loop will ensure that the random numbers will only generate a positive net income.\
\
Here is what the Dynamic Engine would look like:

<figure><img src="https://211323355-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDfKsc0R4AcYm9kZgIT2X%2Fuploads%2F5eqeSP32wSrY9gMzZLB1%2FScreenshot%202024-12-06%20at%202.52.23%E2%80%AFPM.png?alt=media&#x26;token=c5bde51a-5410-467a-9223-c4ed29faaf0c" alt=""><figcaption></figcaption></figure>

Loops are very handy when you want to add some control to the randomized scenarios. In this case, no student will ever receive a question where net income is not above $0.

Here is what a sample question would look like (values highlighted in blue for convenience):

<figure><img src="https://211323355-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDfKsc0R4AcYm9kZgIT2X%2Fuploads%2FbA2RLzoeeokCC6UmcVkg%2FScreenshot%202024-12-06%20at%202.52.34%E2%80%AFPM.png?alt=media&#x26;token=746dd404-c759-432b-9522-939d6f4dec03" alt=""><figcaption></figcaption></figure>
