SPL Programming - 3.1 [Doing loop] Single layer loop
So far, all the code we have written is executed once from top to bottom and then ends. if statements may cause some code to be skipped and not executed. The overall flow of the program is still from top to bottom, and no code will be executed multiple times.
In fact, what computers are best at is repetitive work, that is, to execute the same code over and over. Obviously, we should not copy the code repeatedly, which requires the ability of the program language to loop.
Let’s calculate 1+2+… +100:
A | B | |
---|---|---|
1 | =0 | |
2 | for 100 | >A1=A1+A2 |
Only two lines are OK. After the program is executed, the A1 value becomes the answer we want: 5050.
Almost all programming languages use the keyword for for loop statements, and SPL is no exception.
for 100 in A2 means that the code block of A2 will be executed 100 times. Here, the code block of A2 has only B2, that is, B2 will be executed 100 times. During execution, the cell value of A2 will start from 1, add 1 after each execution, and finally become 100.
A2 cell with for is called loop cell. The code block of A2, that is, the code to be repeatedly executed, is called the loop body of A2. A2 itself is also called the loop variable of the loop, and it will change from 1 to the number of cycles.
Let’s look at this code. We have understood the statement x=x+1 before. A1=A1+A2 in the loop body is similar, that is, each time we add A2 to the current A1, A2 will increase from 1 to 100, so A1 will add 1, 2,…,, until 100, and the operation of 1+2+…+100 is completed.
We can also use this structure to calculate 1+3+5+…+199, that is, the first 100 odd numbers.
A | B | |
---|---|---|
1 | =0 | |
2 | for 100 | >A1=A1+(2*A2-1) |
This time, it’s still to add 100 numbers, so we need to loop 100 times, the loop variable A2 will still change from 1 to 100, but the number to be added is no longer a direct loop variable, and it needs to be calculated through the loop variable (2*A2-1).
Now we do a slightly more complex operation. According to Taylor formula: =1+1/1!+1/2!+1/3!+⋯
, add the first 20 terms to calculate the approximate value of the natural logarithm base e:
A | B | |
---|---|---|
1 | =1 | =1 |
2 | for 20 | >B1=B1*A2 |
3 | >A1=A1+1/B1 | |
4 | =exp(1)-A1 |
Here, we need to introduce an auxiliary variable B1 to store the factorial value n!, It is also calculated step by step in the loop. Finally, the result is accumulated in A1. Use the expression of A4 to see the difference between the calculated result and the actual value. The function exp(1) will calculate the value of e, i.e. the power of e1.
This code can not loop too many times, because n! is going to get very big, and it’s going to get out of the range of floating-point numbers very quickly.
Fibonacci sequence is such a sequence: the first and second terms are all 1, from the third term, each term is equal to the sum of the previous two terms, written out as 1,1,2,3,5,8,…. This is a famous rabbit sequence. If you are interested, you can search its origin. I won’t repeat it here.
Now let’s calculate the nth term of the Fibonacci sequence, n>2.
A | B | |
---|---|---|
1 | =1 | =1 |
2 | 20 | |
3 | for A2-2 | =A1+B1 |
4 | >A1=B1 | |
5 | >B1=B3 |
B1 is the result we need. A2 is the number of terms we want to compute, which is n.
The number of loops in a for statement is not always a constant, but can also be a calculation expression A2-2. Because the first two terms already exist, we only need to calculate n-2 times. There is no loop variable used in this loop body. A3 only controls the number of loops.
Calculate the next item in B3, and then fill in A1 and B1 respectively to replace the original items. In this way, for each loop, A1 and B1 are equivalent to moving forward once in the Fibonacci sequence, from the first and second items to the second and third items, and then to the third and fourth items,… At the end of the loop, they will become the n-1 and n items, and B1 is the result we need.
This code can also be written one line less without the intermediate variable:
A | B | |
---|---|---|
1 | =1 | =1 |
2 | 20 | |
3 | for A2-2 | >B1=A1+B1 |
4 | >A1=B1-A1 |
Debug it and try to find its logic by yourself.
We learned the goto statement in the previous section. In fact, we can use goto and if to create the effect of the for statement. For example, in the first example of 1+2 +…+100:
A | B | |
---|---|---|
1 | =0 | =1 |
2 | if B1<=100 | >A1+=B1 |
3 | >B1+=1 | |
4 | goto A2 |
Here A1+=B1 is equivalent to A1=A1+B1. Similarly, B1+=1 is equivalent to B1=B1+1. This is a simplified way of writing in C language, and this style is retained in SPL. x+=y is equivalent to x=x+y. Similarly, subtraction, multiplication, division can be written like this, but addition is the most commonly used.
Using if and goto to create loops, a loop variable (B1 here) needs to be defined artificially. First, an initial value of 1 is assigned to the loop variable. Then the if statement is used to judge whether the loop ends or not. If not, the loop body will be executed. After execution, the loop variable will be added by 1, and then goto will be used to jump back. If the loop end condition is true, the if statement will no longer execute the loop body.
Obviously, the loop written by the for statement is simpler than the if…goto structure, and it is not easy to make mistakes (if the action of adding a loop variable by 1 is omitted accidentally, the loop created by if…goto will never end). Moreover, such goto violates the principle of not jumping backward. So we don’t usually use if…goto to create loops.
SPL Programming - Preface
SPL Programming - 2.3 [Doing judgement] Comments and jumps
SPL Programming - 3.2 [Doing loop] Multilayer loop
SPL Official Website 👉 https://www.scudata.com
SPL Feedback and Help 👉 https://www.reddit.com/r/esProcSPL
SPL Learning Material 👉 https://c.scudata.com
SPL Source Code and Package 👉 https://github.com/SPLWare/esProc
Discord 👉 https://discord.gg/cFTcUNs7
Youtube 👉 https://www.youtube.com/@esProc_SPL