With esProc, the code for a loop is mainly written with **for*** *statement. The **for** statement will repeatedly execute the code block with *for* being the master cell. There are different formats of *for* statements, as listed below:

## 1. *for* loop

An unconditional loop is the one where the values of the master cell are, in turn, the counts of the current loop, which can usually be terminated by a **break*** *command.

The unconditional loop or endless loop is the simplest loop structure, for example:

A | B | C | |

1 | =demo.query(“select * from EMPLOYEE”) | [] | |

2 | for | =A1(A2) | |

3 | if B2.STATE==”Texas” | ||

4 | >B1=B1|B2 | ||

5 | if B1.len()==10 | ||

6 | break |

In the above example, A2 executes an unconditional loop. B2 retrieves one employee record based on each circle of loop in A2. The ensuing code saves the information of Texas employees in B1.After the information of the first 10 Texas employees are retrieved, the loop will be terminated with a** break** statement in C6. Alternatively, a **break C** command can also be used to replace the

**break**statement to exit from the loop body with cell

**being the master cell.**

*C*After execution, B1 contains the following results:

Note that when using the unconditional loop, we need to make sure that the break statement can be executed properly, otherwise the program will loops endlessly.

## 2. for *A*

Loop members of the sequence *A*. The master cell values will be the current members of *A *in turn.

In esProc, the most commonly used loop is to loop on every member in a sequence. For example:

A | B | |

1 | =demo.query(“select * from EMPLOYEE”) | 0 |

2 | =A1.select(STATE==”Texas”) | |

3 | for A2 | =age(A3.BIRTHDAY) |

4 | >B1=max(B1,B3) |

In this example, all Texas employees are selected in A2. The loop is executed in A3 for each Texas employee to compute their age, and then store the oldest age in B1. When the loop is complete, we can get the maximum age of all Texas employees from B1:

For relatively simple loop statements, sometimes we can use the loop function for sequences to achieve the same result. For example:

A | |

1 | =demo.query(“select * from EMPLOYEE”) |

2 | =A1.select(STATE==”Texas”) |

3 | =A2.max(age(BIRTHDAY)) |

The result of A3 is the same as that in the above example:

## 3. for *n*

Loops *n* times. The master cell values are the current loop counts in turn.

In addition to performing loop on all members in a sequence, specifying the times of loop is also very common.

For example, suppose we have a piece of paper which is 1000mm*800mm in size and of 0.1mm thickness. If we fold it ten times, what will be the final length, width and thickness?

A | B | C | |

1 | 1000 | 800 | 0.1 |

2 | for 10 | >A1=A1/2 | >C1=C1*2 |

3 | if B1>A1 | =B1 | |

4 | >B1=A1 | ||

5 | >A1=C3 |

In the above example, the folding is repeated ten times. After each folding the length will be reduced by half, and the thickness will be doubled. If after the folding the width of the paper is greater than the length, the length and width will be switched. After execution, A1, B1 and C1 contain the length, width and thickness of the paper as follows:

The for *n* statement can also be seen as a simple form of for to(1, *n*) , which means “loop from 1 to *n*“.

## 4. for *a*, *b*, *s*

Loop from *a* to *b*, at the step of *s*. This means “to loop on each member of array to(*a,b*).step(*s*)”. The value of *s* will be 1 by default.

Sometimes the loop does not start from 1, and the step value is not 1. We can then use for *a, b, s* to execute the loop. For example:

A | B | |

1 | for 1,100,2 | >A2=A2+A1 |

2 |

A2 sums up all odd numbers less than 100 and the result is as follows:

For any *n *digits number, if the sum of the *n*th power of each of its digits equals its own, the number can be called a **daffodil number**. In the following example, all three digits daffodil numbers are to be identified:

A | B | C | D | |

1 | [] | |||

2 | for 100,999 | =A2\100 | =(A2\10)%10 | =A2%10 |

3 | =power(B2,3) | =power(C2,3) | =power(D2,3) | |

4 | if B3+C3+D3==A2 | >A1=A1|A2 |

In the loop code in A2, the loop is excecuted only on all 3 digits numbers. After the execution of the program, we can see the result in A1:

## 5. for *x*

Loop when *x* is true. The value of the master cell is the computed result for *x*.

By specifying the loop condition, we can control when to end the loop. For example, the issue in section 1 can also be solved using **for*** *statement with specific loop conditions:

A | B | C | |

1 | =demo.query(“select * from EMPLOYEE”) | [] | |

2 | for B1.len()<10 | =A1(#A2) | |

3 | if B2.STATE==”Texas” | ||

4 | >B1=B1|B2 |

In the master cell of for *x *loop, the cell value is either true or false, which is the result of computing the loop condition every time. At this point we cannot use the values of the master cell as the count of loop. Therefore to retrieve employee records in B2, we use **#C** to get the current number of loops. In **# C**,

*C*is the master cell of the loop body. After the code is executed, the result is the same as that in section 1.

## 6. Nested loops

In the loop body, we can still use the loop statements. This is what we call nested loops, or multi-layer loops. For example:

A | B | C | D | |

1 | [] | |||

2 | for 2,1000 | for A1 | if A2%B2==0 | next A2 |

3 | >A1=A1|A2 |

In the example, A1 stores the prime number sequence we found; A2 loops the integers from 2 to 1000. The prime number sequence is looped in B2. If the number in A2 is divisible by certain prime number, it will be replaced by the next number in A2 with the command of *next A2*. If the number in A2 is not divisible by all integer numbers in current prime number list, a new prime number has thus been found and it will be added to the sequence in A1. The **next C** command in D2 specifies that the rest part of the loop body whose master cell is

*C*should be skipped and the next round of loop begins. After execution, the sequence of prime numbers within 1,000 in A1 is as follows:

When using the ** next **statement, we don’t have to specify the master cell C, but to skip the level of the loop where the cell of

*next*statement is located.

As with the following one hundred chickens puzzle, each rooster is worth 5 dollars, and each hen 3 dollars. Three chicks are worth one dollar. If we can buy 100 chickens with 100 dollars, how many roosters, hens and chicks do we buy actually? We can resolve the issue in the following way:

A | B | C | D | |

1 | [] | |||

2 | for 100 | if 5*A2>100 | break | |

3 | for 100-A2 | =100-A2-B3 | =5*A2+3*B3+C3/3 | |

4 | if D3>100 | next A2 | ||

5 | else if D3<100 | next | ||

6 | >A1=A1|[[A2,B3,C3]] |

In the code, A2 loops on the total number of roosters. We need to judge in B2: if the total price of the roosters is more than 100, there must be too many roosters. Then we can use break command in C2 to end the loop. In B3 we continue the looping to compute the possible number of hens. If the current total price of chicken is more than 100 in D4,we know that there are too many hens. We can use next A2 to increase the number of roosters and try again. In D5, the current total price of chicken is still less than 100, and we can continue to try increasing the total number of hens. The *next *statement here can skip the code for the deepest level of loop, which is the loop having B3 as the master cell. The result in A1 is as follows: