# Examples

`LOOP`

and `IF`

#

Example 1: Modulo with Let's illustrate the following Michelson instructions: `LOOP`

, `DIP`

, `DUP`

, `SWAP`

, `PUSH`

, `CMPLE`

, `IF`

, `SUB`

. The example below computes the *modulo* between two numbers (e.g. 7 % 5 = 2).

The idea is to repetitively subtract the divisor from the dividend. Once the dividend is smaller than the divisor, we have successfully computed the remainder.

The following smart contract takes a pair of integers and computes the *modulo* of these two integers (using a `LOOP`

instruction).

Now, let's simulate the execution of this snippet of code. For this example, the initial stack is initialized with 7 and 5 (in order to compute 7 % 5).

Notice that the `LOOP`

instruction stops when it is given a `False`

condition (it appears in red in the diagram below). In our example, the condition is initialized to `True`

because 7 is greater than 5 (and the *modulo* needs to be computed).

The execution of the "loop_example.tz" smart contract can be simulated with the following command:

`ITER`

and `CMPLE`

#

Example 2: Maximum of a list with Let's illustrate the following Michelson instructions: `CAR`

, `CDR`

, `ITER`

, `DIP`

, `DUP`

, `DROP`

, `SWAP`

, `CMPLE`

, `IF`

, `NONE`

, `SOME`

, `IF_NONE`

, `option`

, `list`

. The following snippet of code computes the maximum of a list of integers.

The idea is to repetitively take an element from the list and compare it with the maximum computed so far. Once the list is processed, the stack contains the biggest integer of the list.

Notice that the storage stores an `option int`

. Indeed, if the given list is empty, then there is no maximum.

Notice, in red in the diagram above, that the `ITER`

instruction is called recursively on each element of the given list of integers.

If you have setup a Tezos node, you can use the tezos-client program to simulate your smart contract (max_list.tz) with the following command.

`EXEC`

and `LAMBDA`

#

Example 3: Executing a lambda with Let's illustrate the following Michelson instructions: `EXEC`

, `LAMBDA`

, `SWAP`

, `PUSH`

. The following snippet of code takes an integer and increments it using a *lambda*.

The idea is to define a lambda function (which increments an integer) and execute the function. Once the function has been applied, the result is returned as a new storage state.

`LOOP_LEFT`

#

Example 4: Computing a sum with The following smart contract illustrates the `LOOP_LEFT`

instruction usage. It takes an integer as the parameter and computes the sum from zero to the given integer.

Let's breakdown the execution of this smart contract. In our example, storage is only used to store the result of the execution. The diagram below shows how the sum of integers from 0 to 5 is computed.

FIGURE 4: Illustration of the `LOOP_LEFT` instruction`LOOP_LEFT`

#

Example 5 : Computing a factorial with Let's illustrate the following Michelson instructions: `LOOP_LEFT`

. The following snippet of code takes an integer and computes a factorial of the given integer; the result is saved in storage.