7,425 Pages

## Conditional execution statements

### if statement

```if(x < 0) {
sign = -1;
} else if(x > 0) {
sign = 1;
} else {
sign = 0;
}
```

Branch the process according to the condition.

```if( (condition) ) {
(these statements are executed, if the condition is true)
} else {
(these statements are executed, if the condition is false)
}
```

### alternative statement

```alternative(x)
case(0) {
x += v;
}
case(1, 2) {
x -= v;
y -= v;
}
others {
y += v;
}
```

This statement is equivalent to the following:

```if(x == 0) {
x += v;
} else if(x == 1 || x == 2) {
x -= v;
y -= v;
} else {
y += v;
}
```

## Iteration statements

### loop statement

```loop {
(statements)
}
```

Infinite loop. (statements) is infinitely executed.

```loop( (number) ) {
(statements)
}
```
```times( (number) ) {
(statements)
}
```

Loop (number) times. e.g.

```loop(4) {
(statements)
}
```

execute the (statements) 4 times. loop statements and times statements are equivalent.

### while statement

```while(x < 0) {
x += 360;
}
```

Conditional loop. In this case, the statement 'x += 360;' is executed while x is negative.

```while( (condition) ) {
(statements)
}
```

The (statements) are executed while the (condition) is satisfied.

### ascent and descent statement

```ascent( (variable) in (first value)..(limit value) ) {
(statements)
}
```
```descent( (variable) in (first value)..(limit value) ) {
(statements)
}
```

For example:

```ascent(i in 0..5) {
array[i] = i;
}
```
```descent(i in 0..5) {
array[i] = i;
}
```

Incremental or decremental loop: the body is executed with the specified variable taking all successive values, respectively in ascending or descending order, between the specified limits (upper limit not inclusive). In the example the variable i is incremented by 1 (ascent) or decremented by 1 (descent) at the end of each loop.

Only the first limit value is included in the iteration because when the (first value) is 0 the (statements) are executed (limit value) times and the values of the variable match the valid indices of an array of (limit value) elements. In the above examples, 0..5 means executing the loop 5 times assigning elements 0,1,2,3,4 of an array of size 5.

The ascent example is equivalent to the following statements:

```array = 0;
array = 1;
array = 2;
array = 3;
array = 4;
```

The descent example is equivalent to the following statements:

```array = 4;
array = 3;
array = 2;
array = 1;
array = 0;
```

Of course, in this example the choice between ascent and descent is inconsequential; using both forms might be useful, for example, to write clockwise and counterclockwise shooting and motion patterns with an identical loop body.

### break statement

```break;
```

Exit a loop. e.g.

```loop {
if(x < 0) { break; }
x += 360;
}
```

is equivalent to the following:

```while(x < 0) {
x += 360;
}
```

break statements are often used with branch statements in order to work out the loop which cannot be worked out with the above simple loop statements.

## Miscellaneous statements

### local statement

```local {
let x = 0;
}
```

Localize the scope and lifetime of the variables which is defined in the block. The variables defined in the block cannot be used out of the block. The variable contents are not guaranteed once the process goes out of the block.

Community content is available under CC-BY-SA unless otherwise noted.