> OctoPerf load testing - While Action documentation page. While Actions loops over its children action as long as its condition is true.

Source: https://api.octoperf.com/doc/design/edit-virtual-user/action-types/logic-actions/while-action/

# While Action

![While until](https://api.octoperf.com/doc/design/edit-virtual-user/action-types/logic-actions/img/while-until.png "While until")

The While Action **runs its children until the _condition_ is "false"**. The _condition_ is evaluated twice, once before executing the actions below it, and once at end of children execution.

!!! Warning
    Be careful since only children of the While Action will be impacted. Use the indentation to check that your While Action is correctly setup.

Parameter name | Description | Required
--- | --- | ---
Name | Descriptive name for this container which is indicated in the bench report. The name is not required, but it is a good habit to name your containers so that you can differentiate them in your bench reports. | No
Condition | This condition must be a variable or function whose value is "true" in order continue processing the children of the While action. If none specified, the result of the last sampler will be used instead. | Yes

## While conditions

![While condition](https://api.octoperf.com/doc/design/edit-virtual-user/action-types/logic-actions/img/while-condition.png "While condition"){ align=left }

Similarly to the If Action, the while conditions must have a direct "true" or "false" value.
Because of this, you cannot directly compare two values. Instead you must either insert a variable or use a script like this one:

```java
${__groovy(${count}<=2)}
```
In this example we take the count variable and evaluate if it is less or equal than two.

<div style="clear:both"></div>

In order to make your life easier, you can activate our condition builder and avoid syntax errors. Check the [condition builder documentation](https://api.octoperf.com/doc/design/edit-virtual-user/action-types/logic-actions/if-action/#condition-builder) for more info.

## While examples

### With counter

![While counter](https://api.octoperf.com/doc/design/edit-virtual-user/action-types/logic-actions/img/while-counter.png "While counter")

!!! Example "While with loop"

    Before the loop it is important to initialize our counter with a first [JSR223 script](https://api.octoperf.com/doc/design/edit-virtual-user/action-types/jsr223-actions/):
    
    ```java
    vars.put("counter","1");
    ```
    
    Now add a WHILE logic action and in the condition, to just limit the number of iterations you can use:
    ```java
    ${__groovy(${counter}<=5)}
    ```
    Just replace 5 by the number of iterations you want.
    
    And now inside the while add another script to increment the counter, make sure to use `groovy` as a language for this one:
    
    ```java
    int counter = vars.get("counter").toInteger()+1;
    vars.put("counter",counter.toString());
    ```

!!! tip
    You can <a href="https://api.octoperf.com/doc/design/edit-virtual-user/action-types/logic-actions/img/whilewithcounter.jmx" download>download a JMX of this example here</a> and import it in your project using the JMeter project option.

### String compare

![While until](https://api.octoperf.com/doc/design/edit-virtual-user/action-types/logic-actions/img/while-until.png "While until")


!!! Example "While with string comparison"

    Here is another example with string comparison, we are running the loop while myvar contains something else than FINISHED:
    ```java
    ${__groovy(!"${myvar}".equals("FINISHED"))}
    ```
    Ideally myvar would be a regex extracting a status from one of the responses inside the loop. This can be used to handle an asynchronous task and wait for feedback by reloading the same page.

!!! tip
    You can <a href="https://api.octoperf.com/doc/design/edit-virtual-user/action-types/logic-actions/img/whileuntil.jmx" download>download a JMX of this example here</a> and import it in your project using the JMeter project option.

### For each

![While foreach](https://api.octoperf.com/doc/design/edit-virtual-user/action-types/logic-actions/img/while-foreach.png "While foreach")

!!! Example "While foreach"

    In this other case we continue the loop until categories_n does not exists:
    ```java
    ${__groovy(vars.get("categories_"+vars.get("counter")) != null)}
    ```
    Note that if categories is a regexp extracting all occurrences, you can also check that counter is less than the matchNr variable:
    ```java
    ${__groovy(${counter} <= ${categories_matchNr})}
    ```
    
    Now assuming you named your variables the same way, inside the while you can go with the following syntax to use the current category: `${__V(categories_${counter})}`

!!! tip
    You can <a href="https://api.octoperf.com/doc/design/edit-virtual-user/action-types/logic-actions/img/whileforeach.jmx" download>download a JMX of this example here</a> and import it in your project using the JMeter project option.
