Here’s how to use for statement.
Description:
Execute a loop according to the specified scope and step.
Syntax:
for a,b,s
Note:
The statement executes the code block in loops from a to b with a step of s.
Parameter:
a |
An integer |
b |
An integer |
s |
An integer to indicate the step. Its value is 1 by default. |
Example:
|
A |
B |
|
1 |
|
|
|
2 |
for 1,11,5 |
|
|
3 |
|
>A1=A1+A2 |
Add up integers from 1 to 11 with a step of 5, i.e. every 4 integers, and store the accumulated value in A1
|
Description:
Loop through a cursor.
Syntax:
for cs,n;x
Note:
The statement retrieves n records from the cursor and returns or retrieves records till x… has changed. Close the cursor once the retrieval is completed. This function is often used to fetch a large volume of records by group. Return all the remaining records and close the cursor when omitting n and x.
Parameter:
cs |
Cursor |
n |
Number of records |
x |
Grouping expression, and cs is sorted by x. With x, n can be ignored. |
Example:
|
A |
B |
|
1 |
=demo.cursor("select * from EMPLOYEE order by SALARY desc") |
=[] |
|
2 |
for A1,10;SALARY |
|
|
3 |
|
>B1.insert(0,A2) |
Loop through the cursor, selecting records with the same SALARY value and inserting them into B1. |
Description:
Start a loop.
Syntax:
for x
Note:
The statement starts a loop executing the code block.
Parameter:
x |
A sequence, an integer, or a logic expression. Loop through the code block against x or to(x), or if x is true. If x is empty, then the loop will be endless. |
Example:
x is an integer:
|
A |
B |
|
1 |
|
|
Save the cumulative total 55 in A1 |
2 |
for 10 |
|
|
3 |
|
>A1=A1+A2 |
Add up integers from 1 to 10
|
x is empty:
|
A |
B |
C |
|
1 |
=0 |
|
|
5050 |
2 |
for |
|
|
Loop will be endless if x is empty. When the loop count reaches 100, then break the loop |
3 |
|
>A1=A1+#A2 |
|
|
4 |
|
if #A2==100 |
break |
|
x is a boolean expression:
|
A |
B |
|
1 |
=15 |
|
|
2 |
for A1>10 |
|
If A1 >10, then start a loop through the code block; otherwise, quit the loop |
3 |
|
>B1=B1+#A2 |
|
4 |
|
>A1=A1-1 |
|
|
A |
B |
|
1 |
=demo.query("select * from EMPLOYEE") |
0 |
Get the oldest age among all Texas employees in B1 after the loop is over |
2 |
=A1.select(STATE=="Texas") |
|
|
3 |
for A2 |
=age(A3.BIRTHDAY) |
A3 performs loop over each Texas employee while computing their age |
4 |
|
>B1=max(B1,B3) |
The oldest age is stored in B1 |