# Control structure - Loops
Picea's loop works the same as PHP original alternative syntax.
There is, however, some minors improvments and a new `{% or %}` clause working much like the `{% else %}` clause in a comparison.
## Loop using `for` / `or` / `continue` / `break` / `endfor`
The simplest of sequence loop, `for` / `endfor` simply iterate over a given counter (or whatever your needs).
**[PICEA]** So, using this code:
```html
{% php $users = array_slice([ 'Tom', 'Sam', 'Mario', 'Steve' ], 0, random_int(0, 4)) %}
User list
{% for $i = 0; $i < count($users); $i++ %}
{% if $users[$i] === 'Steve' %}
{# Will leave the loop if user's name is Steve #}
{% break %}
{% else %}
- {{ $users[$i] }}
{% endif %}
{% or %}
- Given user list was empty
{% endfor %}
```
**[PHP]** Would compile like :
```php
User list
- Given user list was empty
```
**[HTML]** And would render as such given random_int() returns a **3**:
```html
User list
```
**[HTML]** Or would render as such given random_int() returns a **0**:
```html
User list
```
## Loop using `foreach` / `or` / `continue` / `break` / `endforeach`
The more complex `foreach` / `endforeach` allows to iterate over keys and values of an array.
**[PICEA]** So, using this code:
```html
{# Generate a random list of 0 to 4 names #}
{% php $users = array_slice([ 'Tom', 'Sam', 'Mario', 'Steve', 'Joan' ], 0, random_int(0, 5)) %}
Random User list
{% foreach $users as $index => $name %}
{% if $name === 'Steve' %}
{# We skip Steve, but allows Joan #}
{% continue %}
{% endif %}
- {{ $name }}
{% or %}
- Given user list was empty
{% endforeach %}
```
**[HTML]** Could render as such if prior random_int() returns '**2**':
```html
User list
```
**[HTML]** Could render as such given random_int() returns '**0**':
```html
User list
```
## Loop using `while` / `or` / `continue` / `break` / `endwhile`
This syntax allows to loop on a computed iteration count.
**[PICEA]** So, using this code:
```html
{% php $fruits = [ 'apple', 'pear', 'banana', 'tomato' ] %}
Grocery list
{% while $item = array_pop($fruits) %}
- {{ ucfirst($item) }}
{% or %}
- We should never see this, since the list is always populated !
{% endwhile %}
```
**[HTML]** Would render as such:
```html
Grocery list
```
## Loop using `do` / `continue` / `break` / `while`
This syntax do not have an alternative syntax in vanilla PHP.
Picea allows for this unusual syntax to be used the same way you would with any other control structure.
There is, however, no support for the `or` clause for this structure since it will iterate at least once.
**[PICEA]** So, using this code:
```html
Random number generator
{% do %}
{% php $number = random_int(1, 25); %}
{% if $number === 10 %}
{# For a reason I'm not entirely sure why, 10 must not be displayed ! #}
{% continue %}
{% endif %}
- {{ str_pad($number, 2, '0' ,STR_PAD_LEFT) }}
{% while $number !== 15 %} {# Loop stops whenever $number is equal to 15 #}
```
**[HTML]** Could render as such:
```html
Random number generator
- 02
- 12
- 07
- 13
- 04
- 07
- 01
- 16
- 14
- 24
- 14
- 01
- 15
```