SPL Programming - 2.2 [Doing judgement] Branching structure

 

We can make some judgments with the if() function, but in some cases, we need the if statement.

Let’s review the quadratic equation ax2+bx+c=0 we learned in middle school. We can use the formula x=(-b±√(b2-4ac))/2a to find the solutions, but we need to pay attention to b2-4ac . If it is greater than 0, there are two solutions. If it is equal to 0, there is only one solution. If it is less than 0, there is no real solution.

We will program to find solutions of the equation. The coefficients a,b,c will be filled in cells A1,B1,C1, and the resulting solutions are required to be written in cells A2 and B2. If there are two solutions, fill in both A2 and B2; If there is only one solution, only A2 is filled in; If there is no solution, it will not be filled in.

A B C
1 3 8 4
2
3 =B1*B1-4*A1*C1
4 if A3>0 >A2=(-B1+sqrt(A3))/2/A1 >B2=(-B1-sqrt(A3))/2/A1
5 if A3==0 >A2=-B1/2/A1

sqrt() in B4 and C4 is a square root function, which need not be explained in detail. The point is that if statements appear in A4 and A5.


if is followed by a logical expression. If the result of this expression is true, the program will continue to execute the cells to the right of the if cell; If the result of the expression is false, the program will skip those cells to the right of the if cell and go directly to the next line for execution.

Note that this if is not the if()function mentioned in the previous section. It may not be followed by parentheses(if parentheses do exist, parentheses will be regarded as a part of the logical expression, and a layer of parentheses outside the expression will not change the calculation result of the expression), and it must be written at the beginning of the code in the cell.

This is the simplest form of the if statement: if the condition is true, it will be executed to the right, otherwise it will be executed directly down.

There is no error in this code, but there is a small problem. Judge whether A3 is greater than 0 in cell A4, and if it is true, B4 and C4 will continue. And then what? The program will still be executed to A5, and it will be judged whether A3 is equal to 0 again, but this is an unnecessary action and a waste of time. Only if “A3 is greater than 0” is not true, it is necessary to judge whether A3 equals 0.

How to avoid this redundant action? The code can be written like this:

A B C
1 3 8 4
2
3 =B1*B1-4*A1*C1
4 if A3>0 >A2=(-B1+sqrt(A3))/2/A1 >B2=(-B1-sqrt(A3))/2/A1
5 else if A3==0 >A2=-B1/2/A1

The if in cell A4 and the else in cell A5 form a pair. If the judgment in A4 is true, the cells to the right of A4 will be executed. If it is not true, the program will see if there is an else under A4. If there is, it will execute the cell to the right of this else.

else means “otherwise”, that is, the action to be executed when the previous if is not true.

This program uses the formula of quadratic equation, but the premise is that the equation is really quadratic, that is to say, it assumes that A1 is not 0. If A1 is 0, the B4, C4 and C5 of the above program will be divided by 0. Let’s continue to improve this code, so that it can deal with the case that A1 is 0, that is, the equation degenerates into a linear equation.

A B C D
1 3 8 4
2
3 if A1==0 >A2=-C1/B1
4 else =B1*B1-4*A1*C1
5 if B4>0 >A2=(-B1+sqrt(B4))/2/A1 >B2=(-B1-sqrt(B4))/2/A1
6 else if B4==0 >A2=-B1/2/A1

Looking at this section of code, it is obvious that B4:D6 is moved from A3:C5 of the previous section, that is, the else processing to be performed after the judgment in A3 is not true. However, B4:D6 takes up three rows, not just the cells to the right of A4.

At this time, we will introduce the concept of SPL code block.

For any cell, if the lower and the lower left of it are all empty, then the block area formed by the right and the right lower cells of it is called its code block. Take A4 for an example, A5 and A6 below it are all empty, so the block area from B4 to D6 is called A4’s code block. For A3, the A4 below it is not empty, so its code block is only B3:D3, and cannot be extended to line 4 (A4 is not empty). That is to say, all the cells to the right of a cell must belong to its code block, but whether the lower line to the right of the cell belongs to its code block depends on whether the cells below and to the left of it are empty.

It’s rather tongue twister to describe with words, but actually it’s very intuitive from the grid layout. Look at the grid below:

A B C D
1 X X X
2 X X X
3 X
4 X X
5 X X X
6 X X

The code block of A1 is B2:D4, because A2:A4 is empty and A5 is not;

The code block of B1 is C1:D1, because B2 is not empty;

The code block of B2 is C2:D3, because A3:B3 is empty and B4 is not;

The code block of C3 is D3:D3, because B4 is not empty;

The code block of A5 is B5:D6, because A6 is empty.

Code blocks are designed to limit the scope of a statement. If the condition of if is true, the code block of the cell where the if is located will be executed; If not, the code block of the corresponding else cell will be executed.

Some programming languages use curly brackets or BEGIN…END to enclose the statements to be executed. The programming language using this scheme has no requirements for writing format. However, for the sake of readability, people still use text indentation to write the code into this ladder shape to see the scope of each statement. SPL does not use bracket scheme, but directly uses ladder like code blocks to limit the scope of statements, which will force the code to have a certain format.

The above code can also reverse the condition of A3, and the square root calculation can be done only once:

A B C D
1 3 8 4
2
3 if A1!=0 =B1*B1-4*A1*C1
4 if B3>0 =sqrt(B3)
5 >A2=(-B1+C4)/2/A1 >B2=(-B1-C4)/2/A1
6 else if B3==0 >A2=-B1/2/A1
7 else >A2=-C1/B1

The code block of A3 is B3:D6, which will be executed when the judgment condition of A3 is established, otherwise, the code block B7: D7 of A7 where else is located will be executed.

We have to further consider the case of equation degeneration, that is, A1 and B1 are both 0. At this time, if C1 is also 0, then any number is the solution of the equation. If C1 is not 0, then the equation has no solution.

A B C D
1 3 8 4
2
3 if A1!=0 =B1*B1-4*A1*C1
4 if B3>0 =sqrt(B3)
5 >A2=(-B1+C4)/2/A1 >B2=(-B1-C4)/2/A1
6 else if B3==0 >A2=-B1/2/A1
7 else if B1!=0 >A2=-C1/B1
8 else if C1==0 >output(“Any”)

When the equation degenerates to 0=0, the code does not fill in any solution and outputs an any at the bottom right.

Like the above code, else has if in the code block. This situation is not uncommon in reality, which will lead to the wide code, because every additional layer of if needs to be indented to the right, the code is not very beautiful and easy to read.

Many programming languages, including SPL, provide syntax to bind else to if after it, and the above code can also be written as follows:

A B C D
1 3 8 4
2
3 if A1!=0 =B1*B1-4*A1*C1
4 if B3>0 =sqrt(B3)
5 >A2=(-B1+C4)/2/A1 >B2=(-B1-C4)/2/A1
6 else if B3==0 >A2=-B1/2/A1
7 else if B1!=0 >A2=-C1/B1
8 else if C1==0 >output(“Any”)

Write the else and the if immediately following the else together. These else if and the first if form a complete if statement. It is equivalent to judging a batch of mutually exclusive conditions respectively, executing the corresponding code block when a condition is established, and then ending the entire if statement.


SPL Programming - Preface
SPL Programming - 2.1 [Doing judgement] Logic operation
SPL Programming - 2.3 [Doing judgement] Comments and jumps