The SiMPLE Tutorial

(Part 4)

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


In the Part 3, we outlined the fundamental "rules of the game" for programming in Micro-SiMPLE. In Part 4, we will now see how these rules all fit together as we start having some fun playing the "game" of programming.

Loops

Up to now, all of the programs that we've written have consisted of linear sequences of statements. In other words, the computer simply executed each statement of the program by starting at the top of the listing and working its way down, one line at a time, until it reached the bottom of the listing.

If computers could execute the statements of a program only in this linear fashion, it would take an awful lot of instructions to perform certain tasks. For example, suppose you wanted to write a program that would list all of the numbers from 1 to 10. You could write the program in the following manner:

Source Listing

While this may not seem too terribly inconvenient for just 10 numbers, imagine how awkward it would be if you had to list thousands of numbers! Fortunately, there is an easier way. It is referred to as a loop.

In Micro-SiMPLE, loops are generated by using a combination of the "If" statement along with the "Goto" statement. (In Part 6, we will present a much better ways of creating loops. But the "If-Goto" combination represents the most primitive way requiring the least number of keywords).

Let's see how we might generate a very simple loop in Micro-SiMPLE:

Source Listing

When you run the above program, a column of numbers from 1 to 10 will appear on the left-hand side of the screen.

In the previous example, the container named "number" was used to keep track of how many times the loop had been executed at any stage. (Such a container is sometimes referred to as being the index of the loop.) The index started with the value one, and was then successively incremented up through ten in steps of one.

We can easily generalize this idea to create any such controlled loop. To create a forward-running loop whose index takes on successive values starting with "start", and which increments up to "end" in steps of "step", simply use the following general form:

         Set index = start
   label:  --- // Statement(s) to be executed in the loop
         Set index = index + step
         If (index <= end) Goto label

To create a backward-running loop whose index takes on successive values starting with "start", and which decrements down to "end" in steps of "step", simply use the following general form:

          Set index = start
   label:  --- // Statement(s) to be executed in the loop
          Set index = index - step
          If (index >= end) Goto label

Notice that these two general forms are almost identical. (Can you spot the two differences between them?)

Example:

Let's write a program that uses a backward-running loop to launch a rocket from the bottom of the screen. When the rocket gets near the top of the screen, we'll explode it!

Source Listing

Reading Input from the Keyboard

The SiMPLE system library contains two functions which allow numerical data to be entered into a program from the keyboard. These functions are:

"Keyinwhole" and "Keyindecimal"

(Since spaces don't count, these functions are usually written with a space following the prefix "Keyin".)

Generating Random Numbers

The system library contains a function named "Random". This function generates numbers which, for most purposes, are essentially random. To use the Random function, you need to supply a calling parameter which specifies the upper limit of the random numbers you would like to have returned. For example, to return random numbers in the range 0 through 57, you would pass 58 (one more than the maximum random number you would want to have returned) as the parameter.

Let's try out the Random function with the following program:

Source Listing

When you run this program, you will see the following 10 random numbers displayed on a single line:

1 0 33 3 35 21 53 19 70 94

Since we specified the number 100 as the calling parameter to "Random", all the random numbers generated are in the range 0 through 99.

Now let's run the very same program again and see what random numbers are produced this time.

1 0 33 3 35 21 53 19 70 94

We got the very same numbers!!! (B-But... aren't they supposed to be random???)

As we said, the random number generator does not really generate truly random numbers. (It only generates pseudo-random numbers.) The way in which it does this always produces the same sequence of random numbers every time you invoke the random number generator.

You can think of the random number generator as being a very long list of numbers stored somewhere in the computer. (That's not what's really happening, but let's pretend that it is.) Every time you run a new program, the computer starts at the top of this list and, therefore, returns the same sequence of numbers every time. However, there is a way to randomly select a starting point in this "list". Simply invoke the "Randomize" task at the beginning of your program:

Source Listing

Now each time you run the program you will generate a different list of random numbers.

Graphics

Before we can begin discussing computer graphics, we must first talk a little bit about something called pixels.

What is a Pixel ?

A pixel (short for "Picture Element") is a single point in a graphic image. Your computer displays pictures by dividing the display screen into thousands of pixels, arranged in rows and columns. The location of each pixel on the screen is specified by giving the column number (or "x-coordinate") and the row number (or "y-coordinate") at which the pixel exists. (The top-most row number is "zero", and the left-most column number is "zero".)


The abridged system library that Micro-SiMPLE uses contains tasks for displaying in any of 16 possible colors. (The unabridged system library has tasks that will display pixels in millions of colors but, for our present purposes, we don't need to use those many colors.)

The 16 possible colors in which each pixel can appear are specified by a "color number" as shown in the following illustration:

As of Version 10.2.18 and later, each of the 16 colors can usually be expressed either as a number, or by its name:

NumberName
0Black
1Dark Blue
2Dark Green
3Dark Aqua
4Dark Red
5Dark Violet
6Dark Yellow
(or Orange)
7Gray
8Dark Gray
9Blue
10Green
11Aqua
12Red
13Violet
14Yellow
15White

Notice that the first letter of each word in the color's name must be upper case.

Example:

                  Call solid color (Dark Green)
                  Call solid circle (300, 200, 100)

Note:

The use of color names is intended primarily as a convenient way of directly specifying colors to tasks and functions in the system library (as was shown in the above example). However, when used under other circumstances, a color's name might cause a compile error. For example, the statements:

Set c = Dark Green  
Call solid color (c)

will generally not compile (unless the "c" container has been previously defined elsewhere in the program). If you encounter any problems when using a color's name, simply place a pair of parentheses around the name:

Set c = (Dark Green)
Call solid color (c)


The graphics screen has the minimum coordinates (x=0, y=0) located in the upper-left corner of the screen, and the maximum coordinates located in the bottom-right corner. (Notice that the y-coordinates increase as you move down the screen.) The maximum x and y coordinates available to you depend on your current Windows settings. The system library contains two functions ("Xpixels" and "Ypixels") which will retrieve that information.

Another task in the system library is the "Plot" task. Plot will display a pixel of a specified color at a given (x,y) coordinate. The color is specified by first calling the "Pointcolor" task.

The following program will display a red pixel at the center of the screen:

Source Listing

When you run this program, look carefully at the center of the screen. You should see a small red dot. Near the bottom of the screen you will see the message, "Press any key . . .". Pressing any key on the keyboard will end the program. (The "Press any key . . ." message gives you time to view the program's output before it disappears.)

The "Line" task draws a line on the screen in a color specified by the "Linecolor" task. Let's draw two diagonal lines on the display; a red one from the upper-left to the lower-right, and a yellow one from the lower-left to the upper-right:

Source Listing

Once again, when this program terminates we see the "Press any key. . ." message.

Getting Rid of the "Press any key . . ." Message

The "Press any key . . ." message occurs whenever a program terminates without invoking one of the "Quit" tasks. For example, the system library contains a task named "Waitquitkey" that waits for any key to be pressed, and then terminates the program. Let's use Waitquitkey to end our previous program.

Source Listing

This time when you run the program, the display remains quietly intact until you press any key on the keyboard.

Calling Tasks Whose Parameters Return Information

All of the tasks we have considered so far had one thing in common - all of their calling parameters passed information from the calling program to the task. Sometimes it is necessary for a task to pass information back to the calling program. When this is the case, you must supply a defined container for the information to be received from the task. One way to do this is to simply initialize a container somewhere prior to calling such a value-returning task.

For example, suppose you wish to call the "Readmouse" task in the system library. This task has three calling parameters, all of which return information to you from the task. The first two parameters return the current x and y coordinates of the mouse. The third parameter indicates the status of the mouse buttons.

To call this task, you would first need to create three separate containers by storing any number (usually zero) in each container. You can then pass those containers to the task as its calling parameters. For example, the program segment:

         Set  x = 0
         Set  y = 0
         Set  button = 0
         Call read mouse (x, y, button)
         Call output (x)
         Call output (y)
         Call output (button)

would display the current x and y coordinates of the mouse along with the state of its buttons. (If the left-hand button is pushed, a value of 1 will be returned. If the right-hand button is pushed, a value of 2 will be returned. If both buttons are pushed, a value of 3 will be returned.)

Putting More than One Statement on a Line

Programs in SiMPLE generally contain only one statement per line. However you are allowed to put several statements on the same line (if you so choose). Simply separate each of the statements with a semicolon ( ; ). For example, the three statements:

         Set x = 0
         Set y = 2
         Set z = 45

could all be written on one line as:

         Set x = 0; Set y = 2; Set z = 45

Using More than One Line for a Statement

Occasionally you may need to write a program statement which requires more room than can conveniently fit on one line. For example, you may have a complicated mathematical expression involving many terms. SiMPLE allows you to split up a statement onto several lines so that they can be more conveniently read.

To break a statement into several lines, simply terminate each piece with a backslash ( \ ) character. For example, the statement:

   Set answer = first + second + third + fourth + fifth

could also be written as:

   Set answer = first + second \
                + third + fourth \
                + fifth

However, you cannot use a backslash character to split up a line if the point at which you wish to split occurs anywhere between a pair of quotes.

Optional Use of the "Call" and "Set" Keywords

As we said at the beginning, every statement in Micro-SiMPLE begins with a keyword. Statements for invoking tasks begin with the keyword Call. However, it is usually not necessary to specify the keyword Call when making those requests. In other words, statements such as:

   Call rocket (320, 240, 5) @
   Call delay (500)
    - etc. -

can usually be written more simply as:

   rocket (320, 240, 5) @
   delay (500)
    - etc. -

if you so choose. Whenever the name of a task appears by itself, the computer will automatically assume that a Call is implied.

Similarly, it is usually not necessary to include the keyword Set when specifying arithmetic replacement operations. Therefore, statements such as:

   Set x = 5
   Set pi = 3.1416
   Set radius = sqrt (x*x + y*y)
    - etc. -

can usually be written more simply as:

   x = 5
   pi = 3.1416
   radius = sqrt (x*x + y*y)
    - etc. -

if you so choose. (Set and Call are the only SiMPLE keywords that can be optionally omitted.)


This concludes the Micro-SiMPLE portion of this tutorial.
You are now an official Micro-SiMPLE programmer!

You should now take some time to practice writing your own Micro-SiMPLE programs. Try to become familiar with all of the tasks and functions in the System Library (and in the Append Library as well). Only by doing  can you become truly proficient.

Good luck and have fun experimenting with Micro-SiMPLE. (And if you create any really cool Micro-SiMPLE programs, be sure to send us a copy!) Then, whenever you're ready, Part 5 of this tutorial will be waiting for you.

Go to Part 5 of the Tutorial




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

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