SPL Programming - 2.1 [Doing judgement] Logic operation

 

If we want to calculate the absolute value of a number (in fact, SPL has an abs() function, but here we use other methods to try), we need to judge whether the number is greater than 0 or less than 0. We introduce the if() function to achieve this:

=if(x>0,x,-x)

if(a,b,c) function has three parameters, where a is a condition. If the condition holds, b is returned, otherwise c is returned.

Using if function, max(x,y) can also be realized:

=if(x>y,x,y)

Most modern programming languages have a data type called boolean. It has only two values: true and false.

Conditional judgment, such as the above x>0 or x>y, is also understood as a kind of calculation expression in programming language, just like our conventional arithmetic expression, except that its calculation result is a boolean value. When this condition holds, its calculation result is true, otherwise the calculation result is false.

The stricter description of if(a,b,c) function is as follows: if a is true, b is returned; otherwise, c is returned.

To convert a data of other types into boolean, you can use the bool(x) function. If x is not null, it will return true, otherwise it will return false. In fact, there is no distinction between null and false in SPL.

Since Boolean value is also a kind of data, it should be able to participate in operation. Let’s take a more complicated example: given a year, calculate how many days there are in that year.

We know that there are 365 days in a normal year and 366 days in a leap year, so we just need to judge whether the year is a leap year or not. The complete rule is this: if a year is divisible by 4 and cannot be divisible by 100, or it is divisible by 400, it is a leap year. For example, 2004 is a leap year (divisible by 4 but not by 100), 2000 is a leap year (divisible by 400), 1900 is not a leap year (divisible by 4 but also by 100). The SPL expression is as follows (y is the year to be judged):

=if(y%4==0 && y%100!=0 || y%400==0,366,365)

The last two parameters are easy to understand. Let’s explain the first parameter, that is, y%4==0 && y%100!=0 || y%400==0 .

% is used to calculate the remainder of the division operation, == is used to determine whether the operands are equal, y%4==0 indicates whether the remainder of y divided by 4 is equal to 0, that is, whether y can be divisible by 4. If y can be divisible by 4, then the result of y%4==0 is true, otherwise it is false. For example, 2004%4==0 is true, while 1998%4==0 is false.

SPL retained the habit of C language, using != to indicate “not equal to” , while many programming languages use <> to indicate “not equal to”. Other comparison operators in SPL, such as >=, <= and so on, are the same as most programs.

So, y%100!=0 can determine whether y can not be divisible by 100. Similarly, 1900%100!=0 is false, while 1998%100!=0 is true. Similarly, y%400==0 will be used to determine whether y can be divisible by 400.

What are the remaining && and || ?

These are the boolean operators, && stands for “and”, which is usually called AND in programming; || stands for “or”, which is called OR in programming. Both && and || are binary operators, that is, like addition, they have two operands. The operation rules of && and || are as follows:

true && true = true
true && false = false
false && true = false
false && false = false
true || true = true
true || false = true
false || true = true
false || false = false

In fact, they are the normal meanings of “and” and “or”. The priority of && is higher than ||, that is to say, if there are no parentheses, && will be calculated first and then ||, just like multiplication and division before addition and subtraction. If this expression is written as y%400==0 || y%4==0 && y%100!=0, it will get the same result.

There is also a very common operator ! with single operand, meaning “not”, and the operation rules are as follows:

!true = false
!false = true

! is a little bit like the minus sign “-“ in arithmetic, and its priority is higher than &&.

SPL retained the C language operators, while some programming languages directly use the words AND and OR and NOT to express &&, || and !.

Now, we can understand y%4==0 && y%100!=0 || y%400==0, it really correctly expresses the rule of leap year judgment.

Here’s another question. Why do we use two equal signs ==, instead of using one equal sign as in arithmetic expressions?

This is because SPL retained the habit of C language, and considers the assignment statement such as a=x as an operation expression, which also has a calculation result, that is, x. For example, if we write =a=5 in cell A1, SPL will not report an error but will execute normally, and we can see that not only variable a has a value of 5, but also cell A1 itself has a value of 5. Because a=5 is also understood as a calculation formula, its calculation result is 5, which can be assigned to cell A1.

But after such an agreement, if we also use = to judge equality, we can’t distinguish it. Therefore, the C language stipulates that two equal signs == should be used to judge equality, so it will not be confused with assignment operation. SPL also applies this rule.

After getting used to it, we will see the advantages of this convention, which can make the code much simpler. But there is also a risk. It’s easy to write = when it’s time to write ==. Originally, we want to judge the equality, but we made an assignment instead. We should pay special attention to it when writing codes.

It should be noted that not all programming languages are regulated in this way. For example, BASIC language does not regard assignment as an operator. a=5 only completes assignment without a calculation result. It also uses one equal sign to judge equality. The advantage is that it’s not easy to make mistakes, but the code can be cumbersome sometimes.

Still, there is no perfect thing of the kind.


SPL Programming - Preface
SPL Programming - 1.3 [Doing arithmetic] Functions
SPL Programming - 2.2 [Doing judgement] Branching structure