SPL Programming - 1.2 [Doing arithmetic] Variables and statements
The area of a circle is
S=πr^2^. Now we write a piece of code to caclualte the area of a circle with the specified radius. Here we take 3.14 as the value of
Execute the code and view A2’s value, which is the area of a circle whose radius is 5.
To calculate the area of another circle whose radius is 7.8, we just need to change A1 to the specific value and then execute the code. Now A2’s value is the current circle’s area.
It looks like that Excel is more convenient. The spreadsheet tool can automatically calculate after you enter a new value. But this is not the key point. We just let it be.
Let’s examine the expression in A2. It contains a cell name. It references A1’s value at computation and uses the new value whenever there is a change to A1. That is, this expression references a value through the name of cell containing it.
It is easy to understand for one who is familiar with Excel.
Programming languages call named data the variable, like A1 and A2 (We can write =A2*4 in A3 to calculate the area of a ball with A1 being the radius). A3 is also a variable. All cells in the cellset are variables.
Then we can reference a variable in a computing expression. A recomputation of the expression with a new variable value will get a new result. So coding only once for computing multiple expressions of same pattern. It is convenient and error immune. Computers are indeed good at doing the repetitive work.
Excel also has this merit.
Is the variable thus named because it is variable?
That’s it. A variable can get a new value in a program, as the following piece of code shows:
A2 and A4 have same expressions, but they get different results after execution. A3 modifies A1’s value to 7.8 when executed. A4 then takes the new value for A1 to do the computation.
This is different from Excel. The spreadsheet tool cannot modify a cell value by writing a formula in another cell.
Instead of the equals sign =, A3’s code is headed by the sign greater than sign >. In SPL, a cell whose content begins with = is called a calculation cell and one whose content starts with > is called an executable cell. A calculation cell calculates the expression after the equals sign and puts result into the current cell. An executable cell executes the action after the greater than sign but will not put any value into the current cell. In the above code, A3’s value is still null after it is executed.
Cell A1 in the above piece of code, with neither the equals sign nor the greater than sign, is called a constant cell in SPL. It is used to define a constant.
Many programming languages have the concept of constant variable, which can be understood as the named constant. The constant variable value cannot be changed at running time of the program once it is set. The constant variable can also be regarded as a variable that can only got assigned once. The introduction of constant variable aims to avoid changing the variable value that should be kept unchanged when code is wrongly written.
SPL does not have the strict concept of constant variable. The value of constant cell A1 in the above code can be modified by the execution of an executable cell.
Generally, a program written with a programming language is in text format, sentence by sentence. The sentence-wise format is more apparent in programs written in earlier programming languages. Each sentence describes an action to be carried out. Such a sentence is called program statement. A piece of program is made up of a set of program statements.
SPL does not feature this sentence-wise coding format. In a SPL program, code in a cell can be treated as a statement. Both a calculation cell and an executable cell are statements.
In most programming languages, programmers need to name variables. SPL supports user-defined names, too, as shown in the following program:
Here letter r, rather than A1, is used to name the variable. Results are the same.
In the following interface, variable r is displayed in the right bottom section.
The advantage of a meaningful user-defined variable name is unforgettable and not easy to confuse, such as the r used in the above program. It is the same r as the one in the formula for area of a circle.
Sometimes when the variable is used for storing an intermediate result, you can give it a not so meaningful name. But there is a probability that it is a namesake. In SPL, you can name a variable after the cell name. This is one of the small conveniences that the grid-style coding format has but that the text coding format does not have.
There are two variables, a and b, and we need to exchange their values, for instance. The general program is as below:
The newly defined variable c can be renamed after the cell name:
Like in Excel, when the cell name is used as the variable name in SPL and when a row or column is inserted or deleted, the cell name referenced in an expression will be automatically adjusted to get the right result. SPL’s another Excel-like feature is to use the dollar sign $ to fix a cell to the specific row and column when a row or a column is inserted or deleted.
esProc also offers some commands and their shortcuts on the Edit menu. When you are familiar with the SPL features, you will find that its grid-style coding format is far more convenient than the text coding format.
By the way, SPL is a case-sensitive programming language. The lowercase r and the uppercase R generate different effects. The cell name used as a variable name should be the uppercase. A1 represents a cell variable while a1 is only an ordinary variable. Some programming languages are case-insensitive, where the lowercase r and the uppercase R produce same effects.
A sign that can be used as a variable name is usually made up of letters and numbers and must not begin with a number. It can contain the underline (The sign _ is generally treated as a letter and can be placed at the beginning). SPL also allows using the Chinese character in a variable name. Identifiers are the jargon for the signs that can be included in the variable name.
Another question: Does a variable have a data type?
There are two types of programming languages. One is the strong typed languages, such as Java and C/C++. They require that a variable’s data type be declared before it is used. If variable x is declared as the integer type, it cannot be used to store a floating-point number. If it is assigned a floating-point number, it will be automatically converted into an integer (and lost information during the process). The other is the weak typed languages, such as SPL. In SPL, users do not need to declare the data type for a variable; the appropriate type will be naturally generated during the computation.
Does it show that the weak typed languages are more convenient to use?
It does in terms of usage convenience. But as a strong typed language declares the data type of a variable in advance, the computer knows how to store the variable before execution and thus gets a better performance. A weak typed language decides the storage method after the result is obtained, which makes computing performance poorer. Besides, it cannot detect the error if the data type is mistakenly changed.
There is no perfect thing of the kind.
Take the formula for the area of a circle as an example.
S=πr^2 ^ is in essence an equation. It tells that the area of a circle
S is equivalent to
πr^2^. In fact, the equation will still hold if they switch positions.
Now we write the formula in SPL statement to calculate the area of a circle:
Is it an equation too?
In a programming language, a statement that sets the value of a variable is called the assignment statement. It represents an action, rather than being a mathematic equation. It should not be written as follows:
It makes no sense. The computer will report an error for it.
An assignment statement calculates the expression on the right of the sign = and assign the result to the variable on the left the sign =. The calculation of the expression is irrelevant with the variable. The assignment will only begin after the calculation is finished.
This way an equation that is mathematically impossible will become a common assignment statement, as shown below:
It denotes that the current X value plus 1 and the result will be assigned to X. Suppose the X’s value is 3, then its value will become 4 after execution.
You will basically understand the concept of variable if you can figure out what X=X+1 means. This can be counted as the first small barrier in the course of learning program design and a concept that Excel does not have.
Look at this piece of code:
Try to change the initial values of both a and b multiple times and execute the program to see the final values of both variables. You can first make a guess and then try to prove your conclusion using the variable assignment principle.
Early computers have small memories. One fewer variable means less memory usage, and programmers invented the crazy code to achieve this goal.