# 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

``` **[PHP]** Would compile like : ```php

User list

``` **[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

``` **[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

``` **[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

``` **[HTML]** Could render as such: ```html

Random number generator

```