SPL Programming - 3.3 [Doing loop] Conditional loop


for n and for a,b are both loop with definite number of cycles, but sometimes we don’t know how many times to loop. To end the loop only when a condition is true (or not), and repeating the loop body before that, this kind of loop is called conditional loop.

In fact, for n and for a,b can also be understood as such a loop, that is, ending the loop when the loop variable exceeds n and b (exceeding does not necessarily mean greater than). Conditional loop is a more general form of loop.

There is a classical method to calculate the greatest common divisor of two numbers, which is called successive division method. Two positive integers a and b, assuming a>b, replace a with the remainder of a divided by b, now a<b, and then replace b with the remainder of b divided by a, and then a>b,…, and so on, until one of a or b is 0, then the other one that is not 0 is the greatest common divisor of the original a and b. Because a and b exchange positions repeatedly to calculate the remainder, it is called successive division method.

This is the way Euclid, a Greek geometrician, invented more than 2000 years ago. Interested readers can prove it by themselves. We use program code to implement it here.

Set the original two numbers in the cells A1 and B1.

1 7215 2345
2 for B1>0 =A1%B1
3 >A1=B1
4 >B1=B2

The calculation result will be shown in A1.

The code of cell A2 is for B1>0, for is followed by a boolean expression. When it calculates true, the loop body will be executed once, and then return to A2 to calculate the boolean expression again. If it is still true, the loop body will be executed again …; Until the boolean expression is evaluated as false, the loop ends.

This logic can realize Euclidean division. We don’t know how many times to loop, only know that when B1 is 0, the loop can be terminated.

Most programming languages have conditional loops, but they usually use while.

Words like if, else, for will be used in the program code to represent certain special statements, called reserved words or keywords of the program language. These reserved words are of special significance and cannot be used as identifiers such as variable names. All programming languages have a number of their own reserved words.

SPL does not want to have too many reserved words, and it is not ambiguous to use for to represent conditional loop, so it does not retain the more common reserved word while.

Let’s do another exercise related to divisor, prime factorization, which is the content of mathematics lesson in primary school.

The method is very simple. Given a positive integer n, we divide it by 2, if it is divisible, then we get a factor 2. We divide n by 2 to get a new n, continue to divide it by 2, …; until it is not divisible by 2, if n becomes 1 at this time, it’s over. If n is not 1, continue to divide it by 3 and repeat the process; Continue to divide n by 4,5…; in the end, n is always changed to 1, and it’s over.

As we said before, because we haven’t learned set yet, we can output the prime factors we find out.

Let n fill in cell A1 first.

1 7215 =2
2 for A1>1 for A1%B1==0 >output(B1)
3 >A1=A1\B1
4 >B1+=1

Pay attention to that ,which means integer division,is used in cell C3, otherwise floating-point number will be calculated, and the next A1% B1 can not continue.

This is a two-layer loop code. It can also be written as a single-layer loop:

1 7215 =2
2 for A1>1 if A1%B1==0 >output(B1)
3 >A1=A1\B1 next
4 >B1+=1

Replace the for of cell B2 with if, and then add a next in cell D3.

We already know the function of if. What’s the function of next here?

next means to proceed to the next round of loop header and no longer execute the code that has not been executed after it in the loop body. Here, if the code is executed to the next of D3, it means that B4 will be skipped and will not be executed, and the program will directly go to the next loop, that is, it will go back to A2 to judge A1>1 and decide whether to continue to execute the loop body.

Think about this logic process carefully. B1 starts with 2. If A1 can be divisible by 2, output the 2 in C2, divide A1 by 2, and then go back to A2 again to enter the next loop. At this time, B1 is still 2, because next will skip B4, and the statement B1+=1 is not executed. After all the factors of 2 are calculated, the if of cell B2 will not hold, and then it will enter cell B4 to execute B1+=1, change B1 into 3, and then enter the next loop. This single-layer loop can achieve the same effect as the above two-layer loop.

It should be noted that if next is used in the loop of for n or for a,b, the loop variable will still change in the next loop. For example, the previous example of finding odd sum can also be written with next:

1 =0
2 for 200 if A2%2==0 next
3 >A1+=A2

When an even number is encountered, the condition of B2 is established, it will be executed to the next of C2 to enter the next loop, and the loop variable will still be added by 1; When the number is odd, it will be executed to B3 to execute accumulation, and the loop body will be executed 200 times.

In multi-layer loop, next followed by the name of loop cell can make the outer loop directly enter the next round. For example, the Narcissus number mentioned above can also be written as follows:

1 for 9 =100*A1 =A1*A1*A1
2 for 0,9 =B1+10*B2 =C1+B2*B2*B2 if C2<D2 next A1
3 for 0,9 =C2+C3 =D2+C3*C3*C3
4 if D3==E3 >output(D3)
5 else if D3<E3 next B2

It seems that using next achieves the same effect as break, but the mechanism is different. next B2 of cell E5 will directly enter the next round of B2 loop, and if it is break, it will jump out of C3 loop. In this example, the effect of the two is the same, because there is no code of the B2 loop after the C3 loop ends, and the B2 loop ends too.

If the code changes to this:

1 for 9 =100*A1 =A1*A1*A1
2 for 0,9 =B1+10*B2 =C1+B2*B2*B2 if C2<D2 next A1
3 for 0,9 =C2+C3 =D2+C3*C3*C3
4 if D3==E3 >output(D3)
5 else if D3<E3 next B2
6 >output(“B2”)

When E5 code is next B2, C6 will not be executed; If it is break, C6 will still be executed. You can try it and understand the difference by themselves.