Loop Statements

Read(3999) Label: loop statement, forloop,

In esProc, the loop code is mainly refer to the for statement. A for statement repeatedly executes a code block with for being the master cell. There are different formats of for statements: 

2.2.1 for loop

It is the unconditional loop where the master cell value is variable according to loop count. Usually a break command is used to exit the loop.

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

A2 executes an unconditional loop. B2 retrieves one employee record based on loop count in A2. The following code stores records of Texas employees in B1.After records of the first 10 Texas employees are retrieved, the loop is terminated in C6 with a break statement. Alternatively, a break C command, a form of break statement, can be used to exit the loop body that uses cell C as the master cell.

After execution, B1 contains the following result:

Note that when using the unconditional loop, you should make sure that the break statement can be executed properly, otherwise the program will loop endlessly. 

2.2.2 for A

The statement loops members of the sequence A. The master cell values will be the members of A in turn.

In esProc, it’s the most common practice to loop through 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, A2 finds out all Texas employees. A3 loops through each of them to compute the age, and stores the oldest age in B1. When the loop finishes, B1 gets the oldest age of all Texas employees:

Sometimes for relatively simple loop statements, you can use the loop functions for handling 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))

A3’s result is the same as that in the above example:

2.2.3 for n

The statement loops n times, with the master cell values being the loop counts in turn.

In addition to looping through all members in a sequence, it’s also common to specify the times of loops.

Suppose there’s a piece of paper which is 1000mm*800mm in size and 0.1mm in 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 is reduced by half and the thickness doubles. If after the folding the width is greater than the length, then the length and width will switch values. After execution, A1, B1 and C1 get the length, width and thickness of the paper as follows:

   

The for n statement can also be seen as a simple form of the statement for to(1, n) , which means looping through 1 to n. 

2.2.4 for a, b, s

The statement loops through a to b at intervals of s, that is, to loop through each member of the array defined by to(a,b).step(s). The default value of s is 1.

At times a loop does not start from the first member, or the increment value is not 1. You can then use for a, b, s statement 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 nth power of each digit equals the number itself, it is called a daffodil number. In the following example, all 3-digit 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

 

A2’s loop statement loops through only the 3-digit numbers. After the execution of the program, you can see the result in A1:

2.2.5 for x

The statement performs the loop when x is true. The value of the master cell is the result of x.

By specifying a loop condition, you can control when to end the loop. For example, the issue in for loop can also be solved using for statement with a specific loop condition:

 

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

For the master cell of a for x statement, its value is either true or false, which is the result of judging the loop condition every time. Here you can’t get the loop number according to the master cell values. Therefore to retrieve employee records in B2, B2 uses #C to get the current loop number. For #C, C is the master cell of the loop body. When executed, the code gets the same result as that obtained in for loop.

2.2.6 Nested loops

In a loop body, you can still use one or more loop statements to form a nested loop, or multilayer loop. 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 a new sequence of prime numbers to be generated; A2 loops through the integers from 2 to 1000. B2 loops through the prime number sequence. If the number in A2 is divisible by a certain prime number, it will be replaced by the next number in A2 with the command of next A2. If all the numbers in A2 are indivisible by the numbers in the current prime number list, a new prime number has thus been found and it will be added to A1’s sequence. The next C command in D2 skips the rest part of the loop body whose master cell is cell C to execute the next round of loop. After execution, the sequence of prime numbers within 1,000 in A1 is as follows:

When using the next statement, you don’t have to specify the master cell C, just skipping the level of the loop where the cell of next statement is located.

Here is the one hundred chickens puzzle. Each rooster is worth 5 dollars, each hen 3 dollars, but three chicks are worth one dollar. Suppose you buy 100 chickens with 100 dollars, how many roosters, hens and chicks are there? You 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 to get the possible total number of roosters. B2 checks if the total price of the roosters is more than 100; if the result is true, it means that the count of the roosters exceeds the actural number. And end the loop using the break command in C2. B3 continues to compute the possible number of hens by loop. During the process if C4 finds out the current total price of the chickens is more than 100, it means that there are too many hens. Then D4 uses next A2 to increase the number of roosters to try to achieve the balance. D5, however, skips the current innermost loop defined by cell B3 using the next command to increase the number of hens if C5’s result shows the current total price of chicken is still less than 100.

A1 gets a result as follows: