Loop Code for esProc

Uncategorized 724 0

With esProc, the code for loop is mainly implemented with for statement. The for statement will repeat the code block with for as the main cell. There are different formats of the for statements, as listed below:

1. The for loop

Unconditional loop. The values of the main cell are, in turn, the count for the current loop, which often needs to be exitedby break command.

Unconditional loop or endless loop is the simplest structure, for example:esProc_loop_1

In the above example, A2 executes unconditional loop. According to the loop number in A2, B2 retrieves one employee record every time. The rest of codes savethe employee information for Texas in B1.After the information for first 10 Texas employees were retieved, the loop will jump out with break statement in C6. Alternatively, break C command can also be used to replace break statement to jump out from the loop body with cell C as the main cell.

After execution, B1 contains the following results:

esProc_loop_2

Note that when using the unconditional loop, we need to make sure that the break statement can be executed properly, otherwise theprogram will never end at the time of execution.

2. for A

Loop the member of the sequenceA, the main cell value will in turn be the current member of A.

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

esProc_loop_3

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 for all Texas employees from B1.

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

esProc_loop_43. for n

Loop n times;with the main cell value as the current loop count in turn.

In addition to loop on all members in a sequence, loop for specific times isalso very common.

For example, suppose we have 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?

esProc_loop_5

In the above example, the folding is looped 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 contains the length, width and thickness of the paper as following:

esProc_loop_6

The for n statement can also be seen as a simple form of for to (1, n) . This 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)”. If s is absent the default value will be 1.

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

esProc_loop_7

For any n digits number, if the total ofthe n-th power of each digit equals its own, the number can be called daffodils number. The following example is used to identify all three digits daffodils number:

esProc_loop_8

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

esProc_loop_95. for x

Loop when x is true. The value in main cell is the computed value 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 with for statementwith specific loop conditions:

esProc_loop_10

In the main cell of for xloop, the cell value is either true or false, which is the result we got after computing the loop condition every time. At this point we can not use the value from main 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 main cell of loop. After the code is executed, the result is the same as in section.

6. for cs,n:x

Loop cursor, and retrieve n records each time, or retrieve records until x has changed. Return table sequence. Close after the reading.

When retrieve the data with cursor, we can use for statement to loop the data in cursor.

esProc_loop_117. Nested loop

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

esProc_loop_12

In the example, A1 stores the prime number sequencewe found; A2 loops the integer 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 number in current prime number list, we then find a new prime number. It will be added to the sequence in A1. Among them, the next C command in D2 specifies to skip the rest part of the loop body and enter the next round of loop. After execution, the sequence for prime numbers within 1000 are as following in A1:

esProc_loop_13

When using the next statement, we don’t have to specify the main cell C, to skip the deepest level of the loop where the next statementis located.

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

esProc_loop_14

FAVOR (0)
Leave a Reply
Cancel
Icon

Hi,You need to fill in the Username and Email!

  • Username (*)
  • Email (*)
  • Website