# While And Until

## While

* The `while` statement repeats a block of code while a condition is true.

### Example

```js
while boolean (
   command
   command
)
```

### Syntax

```js
while condition (
    commands
)
```

* `condition`: The condition that determines whether to continue executing the block of commands.
* `commands`: The commands to be executed within the loop.

### Example

```js
while distance_to_object < 10 (
    robot "moveForward" 10
    robot "turnLeft" 10

    robot "get_data"
    distance_to_object = robot."sensor1"
)
```

In this example, the robot will keep moving forward and turning left as long as there is an obstacle ahead.

## Until

* The `until` statement repeats a block of code until a condition is true.

### Example

```js
until boolean (
   command
   command
)
```

### Syntax

```js
until condition (
  commands
)
```

* `condition`: The condition that determines whether to stop executing the block of commands.
* `commands`: The commands to be executed within the loop.

### Example

```js
until isGoalReached (
  // do things to get to goal
)
```

In this example, the robot will keep moving forward until the goal is reached.

### Use Cases

1. **User Input Validation with `while`**

```js
// Ask for user input until a valid value is entered
userInput = null
while userInput != null (
  userInput = "hello".ask()
  // keep asking until the user answers
)
```

This example demonstrates how the `while` statement can be used to repeatedly prompt the user for input until a valid value is entered.

2. **Waiting for External Event with `until`**

```js
// Wait until a sensor detects an object
objectDetected = false
until objectDetected (
  robot "get_data"
  objectDetected = robot.detected
)
```

Here, the `until` statement is used to wait until a sensor detects an object before proceeding with the next set of commands.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://osl.mistium.com/program-flow/while-and-until.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
