# whileLoop

## Description

Loop runs, and it continues to repeat as long as the condition remains true, it evaluates the condition first before looping.

## Input / Parameter

| Name      | Description                                  | Input Type | Default | Options | Required |
| --------- | -------------------------------------------- | ---------- | ------- | ------- | -------- |
| condition | The condition to evaluates first before loop | boolean    | -       | -       | Yes      |

## Output

N/A

## Callback

### yesCallback

This will be triggered for every loop that happens while the condition remains true.

## Example

In this example, we will do console logging by using `console` function in `whileLoop` callback

### Code Equivalent

```
let n = 0;
while (n < 5) {
  n++;
  console.log("while loop"+n);
}
```

### Steps

1. Drag a `button` component into the canvas and open the `Action` tab. Select the `press` event of the button and drag the `setVar` function to the event flow, add the value `n` on `var` param and add the value `0` on `value` param.

   ![](https://399701567-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwmJ1RKjM2uNFaL6fO3Xu%2Fuploads%2Fgit-blob-f1ae801c5d252fe14bc742bc7f40216211253e1b%2FwhileLoop-step-1.png?alt=media)
2. Drag the function `whileLoop` to the eventflow, on it's `condition` param change the param type to function/subflow then add function `less` inside the subflow, for the `value1` param change the param type to function/subflow then add function `getVar` inside the subflow, for the `var` param add value `n`.

   ![](https://399701567-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwmJ1RKjM2uNFaL6fO3Xu%2Fuploads%2Fgit-blob-dba411ac2cbb4d8e98a44cd4047e6f7bb9a068fc%2FwhileLoop-step-2.png?alt=media)
3. Drag the function `setVar` inside function `whileLoop` yes callback, add value `n` on param `var` and change the param type to subflow/function on param `value`. Inside param `value` subflow add `add` function then on it's `value1` change the param type to subflow/function, add `getVar` function in it, then put `n` on it's `var` param value.

   ![](https://399701567-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwmJ1RKjM2uNFaL6fO3Xu%2Fuploads%2Fgit-blob-614c8f619162b1a5186f7320dfff3fcf0187d591%2FwhileLoop-step-3.png?alt=media)
4. Drag the function `console` inside function `whileLoop` yes callback, change the param type to subflow/function on param `value`. Inside param `value` subflow add `plus` function then on it's `value1` param add `while loop` value, as for `value2` param change the param type to subflow/function, add `getVar` function in it, then put `n` on it's `var` param value.

   ![](https://399701567-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwmJ1RKjM2uNFaL6fO3Xu%2Fuploads%2Fgit-blob-926ffc421e62c66dad3b2b8db4344a97f606a33d%2FwhileLoop-step-4.png?alt=media)

### Result

1. Now click the button in preview, it will show the list of `while loop` with the current loop number on the console.

   ![](https://399701567-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwmJ1RKjM2uNFaL6fO3Xu%2Fuploads%2Fgit-blob-621647be8d67606d3ab68d2cb6efad709c09d0ba%2FwhileLoop-result-1.png?alt=media)

## Links

### Related Information

See also:

* Functions
  * [plus](https://docs.emobiq.com/emobiq-client/006-actions-and-visual-logic/action-reference/cordova/math/plus)
