The SiMPLE Tutorial

(Part 3)

("The Fun and Easy Way to Become a Programmer")


Before we can continue writing our Micro-SiMPLE computer programs, we're going to have to cover some material that might seem a little "boring" at first. But hang in there! We'll get back to the "fun stuff" shortly.

Whole Numbers and Decimal Point Numbers

The computer recognizes two different types of numbers: whole (or integer) numbers, and decimal point (or floating point) numbers.

Whole numbers are simply numbers that do not contain a decimal point. For example:

6, 23, -5, 0, 32000, etc.

are all whole numbers. Whole numbers cannot be greater than 32767 or less than -32768.

Decimal point numbers, on the other hand, do contain a decimal point. For example:

3.1416, 23.0, -.005, 0.0, etc.

are all decimal point numbers. Decimal point numbers are limited to no more than seven decimal places of accuracy.

Even though the numerical value of a whole number may be identical to its corresponding decimal point number (for example: 23 and 23.0), the computer handles these two types of numbers completely differently.

Arithmetic

One of the primary functions of a computer is to perform calculations. (After all, that's why they call it a "computer"!)

Also, computers have the ability to store lots of numerical information. This information is stored inside of the computer's memory. SiMPLE partitions your computer's memory into individual compartments. We will refer to these compartments as containers.

Containers and How to Use Them

A container can be thought of as being like a little match box with a gummed label on it. Inside the matchbox is a small slip of paper capable of containing a single numerical quantity, such as: 27, or 3.1416, or -10000, etc. (Of course, we all know that computers don't really use match boxes and slips of paper, but we can pretend that they do.)

The label on the box can be any sequence of lower-case letters (a - z) and digits (0 - 9). Additionally, the first character of a container name must be a letter. The number of characters in a name can range from 1 to 20. (You can also include spaces anywhere in a name, since spaces are ignored.) Some examples of legitimate names you can use for containers are:

      game piece
      elvis
      hi score
      j27w
      r2d2
      call

As illustrated in the last of the above examples, a container can have the same name as a keyword (in this case, "Call"). Since keywords always begin with an upper-case letter, and container names always begin with a lower-case letter, there is never any confusion as to whether a given word represents a container or a keyword.

Some examples of illegitimate names are:

      John         (should not contain an upper-case letter)
      23miles      (cannot begin with a digit)
      mr.logic     (cannot contain a decimal point)
      high_score   (should not contain an underscore)

Keyword: Set

To create a container and store a number in it, all you have to do is use the keyword "Set", followed by the name of the container that you want to have created, followed by an equal sign, followed by the quantity that you want to put into the container.

For example, the statement:

Set fish = 5

tells the computer to automatically create a container for you, label it with the name "fish", and put the number 5 inside it. If a container named "fish" was already defined in your program (in a previous statement), the computer will use that container (and not create a new one). It will then throw away any number that might already be inside the "fish" container and insert the new number 5.

The containers that you create exist only while your program is running. When the program ends, all the containers that it used are discarded.

Displaying the Contents of a Container

One way to display the number 27 would be as follows:

Call output (27)

Another way to display the number 27 would be to first store the value 27 into a container, and then display the contents of that container:

Set x = 27
Call output (x)

Therefore we see that, in addition to being able to display explicit numbers and words, the "Output" task can also be used to display the contents of a container. Simply Call it with the name of the container as the calling parameter.

The "Output" task always issues an automatic carriage return when only one calling parameter is supplied. To suppress the carriage return, simply include a second calling parameter. This second parameter specifies how many spaces (on the same line) are to follow the displayed information.

For example, the following program:

Source Listing

would display:

The answer is 42

Arithmetic Expressions

An arithmetic expression is a combination of numbers and/or containers and/or functions, along with such arithmetic operators as plus sign, minus sign, etc. Parentheses can also be used (to indicate grouping). An expression can be very complicated, involving many terms inside parentheses inside of other parentheses. Or an arithmetic expression can be as simple as a single constant or a single container.

Arithmetic operations are specified by the following symbols:

         +    (Addition)
         -    (Subtraction)
         *    (Multiplication)
         /    (Division)
         ^    (Power)
         %    (Modulo) [Remainder after division]

Below are some examples of arithmetic expressions:

         t
         x + y + z
         cat * mouse
         (cost - expense)/100.0
         z^2 - x^(y-1.3)
         number % 10
         42

General Form of Arithmetic Statements

Every arithmetic statement in SiMPLE has the following general form:

Set name = arithmetic expression

where:

Set - is a keyword which indicates that an
            arithmetic replacement statement follows.

name - is the name of the container that you
            want to use to hold the result.

= - is the replacement operator.

arithmetic expression - is a calculation to be performed

The following are some examples of arithmetic statements:

         Set x = 5.4321
         Set a = b - c
         Set radius = Sqrt (x^2 + y^2)
         Set final answer = 42
         Set x = (-b + sqrt (b * b - 4 * a * c)) / (2 * a)
         Set hue = random (14) + 1
         Set even prime number = 2
         Set z = z + 1

The third and fifth examples reference the "Sqrt" function (in the system library) for computing square roots. The sixth example in the above list references "Random", the function for generating random numbers. The SiMPLE system library contains a number of such functions for performing arithmetic operations. And just as in the case of tasks, all of the names of these functions can begin with either an upper-case letter or a lower-case letter.

Furthermore, functions can have calling parameters just as tasks do. When specifying calling parameters to a function they are passed inside of parentheses, just as when calling a task. (If a function has no calling parameters, an empty pair of parentheses must be supplied.) Notice that calling parameters are not limited to being only constants but can be any arithmetic expression.

Notice also that the last example shown above is perfectly valid, because the equal sign does not represent "equality". In an arithmetic statement, equal sign means "replace the contents of the container specified on the left side of the equal sign with the value obtained by evaluating the expression on the right side of the equal sign." Therefore, the expression, Set z = z + 1, simply means "examine the number currently contained in 'z', add 1 to that number, and store this new result back into the same 'z' container."

Containers and the Types of Numbers They Can Hold

Once you initially store a number in a container then, for the duration of the program, that container can hold numbers only of the same type as the initial number you stored.

For example, if we initially store a decimal point number in a container, then that container can only be used for storing decimal point numbers. If we later attempt to store a whole number in the container, the computer will automatically add a decimal point before storing it.

Similarly, if we initially store a whole number in a container, then that container can only be used for storing whole numbers. If we later attempt to store a decimal point number in the container, the computer will automatically truncate (throw away the fractional portion of) the number before storing it. [Note: "Truncate" does not mean the same thing as "round off".]

For example, consider the following program:

Source Listing

The results will be:

55
33

Even though we tried to store the number 33.7654 into the container labeled "speed", the computer wouldn't let us do it. This is because the "speed" container had been initialized to hold only whole numbers (by virtue of the fact that we initially stored the whole number 55 in that container). The computer therefore truncated the value 33.7654 to 33.

Use of Spaces and Blank Lines

As you already know, spaces are essentially ignored in SiMPLE. The only time spaces "count" are when they appear inside of quotation marks. For example, the statement:

Cal lout put ("SiMPLE is Wonderful!")

is exactly the same as:

Call output ("SiMPLE is Wonderful!")

However, the statement:

Call output ("Si MP LE is Won derf ul!")

is not the same as:

Call output ("SiMPLE is Wonderful!")

Similarly, blank lines (produced by pressing the 'Enter' key with nothing else on the line) are also ignored by SiMPLE.

Comment Lines

As programs get longer and longer, they also have a tendency to become more complicated - especially when you have to go back and try to understand the program listing at some future time. Therefore it will become increasingly important to be able to insert comments into your programs so that you can leave notes to yourself about how the code works.

The start of a comment is indicated by two consecutive slashes (//), followed by the comment you wish to leave for yourself. Comments can be introduced either by starting a line with the two slashes (in which case the entire line is regarded as being a comment statement), or else by ending an existing statement with two slashes, followed by the comment:

                   //Here are some examples of comments
   Set x = 3       //This statement stores a three in "x"
   Set y = x + 2   //This is just a useless comment
   Call output (y) //This statement will cause a "5"
                   // to be displayed on the screen

Another kind of comment is indicated by a slash-asterisk (/*), followed by the comment, followed by an asterisk-slash (*/). This form allows you to spread a comment over as many lines as you like:

   Set x = 3
   Set y = x + 2
   /* This comment
   extends over
   three lines. */
   Call output (y)

Comments can begin in any column and are ignored by SiMPLE, as are blank lines and unquoted space characters.

Logical Expressions

We have already defined the concept of an "arithmetic expression". (Recall that arithmetic expressions consist of the symbols: "+", "-", "*", etc., along with numerical constants and containers). We will now present the concept of a "logical expression".

A logical expression is a True or False statement about the relationships between two or more entities.

For example, the statement "x is less than y" is a logical expression. If the number in the container labeled "x" is less than the number in the container labeled "y", then the logical expression is True. If the number contained in "x" is not less than the number contained in "y", then the logical expression is False. In SiMPLE, this logical expression can be specified as:

x < y

The following table summarizes the types of relationships that can be used in logical expressions and how these relationships can be specified in SiMPLE:

         <    (less than)
         <=   (less than or equal to)
         =    (equal to)
         !=   (not equal to)
         >    (greater than)
         >=   (greater than or equal to)

Notice that logical expressions use the same "equal sign" symbol as is used in arithmetic statements. However, when used in a logical expression, equal sign is not used as a replacement operator. Instead, equal sign represents a test for actual equality.

Furthermore, the "not" operator ( ! ) can be used to negate an entire logical expression. For example:

!(x >= y)

is exactly the same as:

x < y

Keyword: If

The general form of an "If" statement is:

If (logical expression) statement1; statement2; . . .

where:

If - is a keyword which begins the "If" statement.

logical expression - is a logical expression.

statement1; statement2; . . . - are one or more statements (separated by semi-colons) to be executed if the logical expression is true.

For example, suppose you would like to create a statement in your program that will check the number that is currently in a container labeled "fun". If the number in "fun" is less than zero, you would like to replace that value with a zero, and then display the words: "NO FUN"

The following statement will accomplish this result:

If (fun < 0) Set fun = 0; Display "NO FUN"

When specifying an "If" statement in SiMPLE, none of the statements to be executed (if the logical expression is true) can be another "If" statement.

Keyword: Goto

The "Goto" statement provides a way of redirecting a program's flow of execution.

The general form of a "Goto" statement is:

     Goto label
              ---
              ---
label
: statement

where:

Goto - is a keyword.

label - is the name of a label in your program.

statement- is the statement to be executed next.

The rules for naming a label in your program are the same as those for naming the label on a container. (It must start with a lower-case letter, followed by any sequence of lower-case letters or digits.)

For example, in the following program:

Source Listing

the "Goto" statement in the third line branches over the "Call output ("Zap")" statement in the fourth line (since the number in container "a" is less than 10). As a result, the only message that will appear on the screen when you run the program is:

The End

Notice that when defining a label (as in the fifth line of the program), the label (done) must be followed by a colon. But when referencing the label in a "Goto" statement (as in the third line of the program), the name (done) is not terminated by a colon.

A label can exist all by itself (i.e., it does not need to immediately precede a statement on the same line). If a label does not have a statement following it on the same line, the label then refers to the statement on the next non-blank line.

For example, our previous program could also have been written as:

Source Listing

Spaghetti Code

It is strongly advised that you avoid indiscriminately using "Goto" statements in your program unless it is absolutely necessary. The misuse of excessive "Goto" statements can lead to what is sometimes referred to as "spaghetti code". However, in order to minimize the number of keywords in the language, Micro-SiMPLE relies somewhat heavily on the "Goto" statement, especially for creating loops (which we will discuss shortly).

Recommended Format For Programs

Even though blank spaces "don't count" in SiMPLE, we recommend that beginning programmers use three separate "columns" when writing the statements of a Micro-SiMPLE program. The left-most column would be reserved for defining the statement's label (if any). The next column would contain the initial keyword for the statement. The final column would contain the rest of the statement. Using this format, our previous example program would be written as:

Source Listing

It is not a requirement that you use this format, but doing so will help you to remember the fundamental structure of Micro-SiMPLE programs. It will also make your programs easier to read.

Go to Part 4 of the Tutorial




  [ Contact Us | Homepage | Online Forums | About the Author ]

© Copyright 2007 SiMPLE CodeWorks, Inc. All rights reserved.