SPL Programming - 3.4 [Doing loop] Endless loop

 

What happens if you don’t write anything after for, neither the number of loops nor the loop condition?

This kind of loop is called unconditional loop, commonly known as endless loop. In theory, this loop will be executed endlessly and never stop, just like goto which is not used with if.

This obviously doesn’t make sense. We shouldn’t write a program that can’t be terminated.


However, it’s not uncommon to see endless loop code in reality. What’s the matter?

Because we also have break and if.

An endless loop is not really an endless loop, only that when we write a loop statement, we are not sure how to write the end condition of the loop. It needs to be determined during the running process of the loop body. In this case, we will write an endless loop at the beginning of the loop, and use if to judge the end condition of the loop in the loop body. If it is true, we will use break to jump out of the loop.

A real endless loop really doesn’t make sense. However, if the if used to trigger a break is wrongly written, the pseudo endless loop may become a real endless loop, which should be paid special attention to when coding.


Set a four digit x, split the four digits respectively, use these four digits to form the largest four digit a, and then form the smallest four digit b. Here, we also consider the first few digits that are 0 (actually three digits, two digits or even one digit) as reasonable four-digit numbers. Then calculate a-b to get a new four-digit number, regard it as x and repeat the process, so that the calculation can be repeated infinitely.

If the new x is the same as the previous x in a certain round of calculation, it is obvious that if the calculation continues, only the same x will appear, and it is meaningless to continue. We call x at this time “digital black hole”, once the number of black hole appears, the operation can be stopped.


Write a piece of code with endless loop, randomly choose a four digit x as the starting value, take finding the number of black hole as the ending condition, and output all the x in the process.


A B C D E
1 1234



2 for =A1\\1000 =A1\\100%10 =A1\\10%10 =A1%10
3
=max(B2,C2,D2,E2)
=B2+C2+D2+E2-B3 =min(B2,C2,D2,E2)
4
if B3==B2 >C3=max(C2,D2,E2) >D3-=(C3+E3)
5
else if B3==C2 >C3=max(B2,D2,E2) >D3-=(C3+E3)
6
else if B3==D2 >C3=max(B2,C2,E2) >D3-=(C3+E3)
7
else >C3=max(B2,C2,D2) >D3-=(C3+E3)
8
=B3\*1000+C3\*100+D3*10+E3 =B3+C3\*10+D3\*100+E3*1000
9
>output(A1) =B8-D8 if C9==A1 break
10
>A1=C9



The initial value is placed in A1.

Because we haven’t learned the knowledge of set, we can’t use the function of set sorting. The most troublesome part of this code is B3:E7, which is used to sort the four digits from large to small. B2:E2 solve the four digits respectively. The max/min function is used to calculate the maximum digit in B3 and the minimum digit in E3. D3 is the sum of three digits except the maximum digit. Then, B4:B7 respectively judge which of B2:E2 is the largest digit, the remaining largest is the second largest digit, and then subtract it and the smallest from D3 to get the third largest digit. The final B3:E3 are the four digits from large to small. If we have the knowledge of set, the sorting can be completed in one cell.

However, it’s good to review the if… else structure again in this troublesome way.

The following code is simple. Calculate the maximum and minimum four-digit that can be formed respectively, and subtract to get a new x. if it is the same as the previous one, the loop will be ended. Otherwise, the new x will replace the old x to continue the loop.

Try it on your own to see if this code can stop and which number can it stop at?

Again, if the program does not end due to wrong code, you can use the debugging function to force it to stop.


Up to now, we have known several structures to control the flow of program:

1)Sequential structure: execute from top to bottom

2) Branching structure: according to the conditions to determine the different direction, will skip some code, the overall direction is still forward

3) Loop structure: repeatedly executing a piece of code, including loops formed by goto


If we do not consider the function call, we can say that all programming languages have only such a few process structures. We have learned them all.

If we review the examples we have done, we will find that the programming language does not help us find a way to solve the problem. When we encounter a problem, we still need to come up with a solution ourselves, that is, to solve the problem through a series of steps (using the combination of the above three basic structures), which are usually called algorithms.

The function of programming language is to provide a way to describe the algorithms that human beings think of, so that the computer can understand and execute. A good programming language can more succinctly describe people’s ideas, and programming will be fast; A bad programming language may be very troublesome, and it will be more difficult to think of a solution description than a problem solution.

In any case, don’t expect programming languages to help us come up with algorithms. If you can’t think of an algorithm, there is no solution to the problem.


SPL Programming - Preface
SPL Programming - 3.3 [Doing loop] Conditional loop
SPL Programming - 4.1 [Sequence] Sequence