NOTE: Use the palgo version on the web here, not the version on the CS that came with the lab book. The version on the web has a number of bugs fixed.
Contents:
Preface: While studying High-Level Programming, we will be using software provided by the book, Explorations in Computer Science: A Guide to Discovery. The Palgo software is available online and it is discussed by the author in Lab 8 of the corresponding text. In the remainder of this document, we have added our own comments.
We offer some words of warning. Though the software is fun to play with, we have already noticed some unusual behaviors. The software seems to work properly when you give it proper commands, but it does not give you useful feedback when you give it illegal commands (most commercial languages do give you such support!)
If you wish to "load" a program which you've written elsewhere, you should be able to copy/paste in a fashion similar to that used with Super Simple CPU. Similarly, if you have written a program in the Palgo edit window and you wish to save it to another file, you should be able to copy/paste in reverse fashion.
Please keep in mind that this software was very recently developed and presumably may be better polished in the future.
The software uses a specially designed programming language which is meant to be similar to some commonly used imperative languages (though hopefully simpler!).
The program maintains an imaginary "pen" with the following "state" information:
The only way that squares will be drawn is by explicitly using the "draw" command, or by having the pen in the down position and then moving the pen with the (left, right, up, down) commands.
Example:
draw(x, y) /* legal */ draw (x, y) /* wrong */
This resets the grid to have "num" rows and "num" columns
(the default grid is 20x20).
Notes:
This resets all squares back to the background color (white), effectively clearing the screen.
This changes the current color of the pen to the color with the
specified name. The color name must be within quotation marks and
uncapitalized.
Notes:
This changes the current color of the pen to the color with the specified red/green/blue value. Each of the three values should be in the range [0,255].
This jumps the pen location to the specified location, however it does not cause any squares to be drawn.
This has the effect of moving the current location of the pen
the specified number of squares to the left.
Notes:
This outputs the expression to the user. It can either be a
character string within quotes or a numeric value. If the expression
is a variable name, then the current value of that variable is
printed.
Note:
This reads a numeric value from the user and stores it in the specified variable.
This reads a character string from the user and stores it in the
specified variable.
Notes:
This causes a delay of (approximately) the specified number of milliseconds.
This sets the variable to a randomly chosen integer in the range [0,num-1] for the given parameter num.
Not actually a command. You can place comments within your program in this fashion. A comment can be on a line of its own, or it can be placed at the rightside of another command.
This assignment sets the value of a chosen variable. The variable name can be almost anything you wish. The only exceptions are that you cannot use keywords which already have meaning in the language (such as left, draw, or while). The expression on the right side of the assignment statement can be a string (in quotation marks), an integer, a name of another variable, or an arithmetic expression which will be evaluated at that time. The right side expression might even involve the variable you are setting, in which case the right side is evaluated first, and then the calculated value is stored in the variable (e.g., mycount = mycount + 1).
if <boolean expression> then ... endSyntax Example:
if count==5 then right(); endThe boolean expression is evaluated. If it is a true statement, then the "body" of the statement will be executed. If the original expression was false, then the body is skipped.
if <boolean expression> then ... else ... endSyntax Example:
if count==5 then right(); else up(); endThe boolean expression is first evaluated. If it is a true statement, then the first part of the "body" will be executed and the second part skipped. If the original expression was false, then the first part of the body is skipped yet the second is executed. (further discussion can be found on pp. 242-243 of the Dale/Lewis text).
repeat <num> times ... endSyntax Example:
repeat 8 times down(1) right(1) endThe body of the expression will be repeated exactly num times, where num can be an explicit value or a variable name.
for <variable> = <numA> to <numB> ... endSyntax Example:
for i = 5 to 10 draw(19,i) endSimilar to a repeat loop. However a variable is defined which is explicitly incremented from numA to numB. The body of the loop can then refer to the value of the variable.
while <boolean expression> ... endSyntax Example:
while i>10 down(1) i = i/2 endThe flow of control of a while statement is as discussed on pp. 246-250 of the Dale/Lewis text.
define <subroutinename> (<list of parameter names>) ... return <answer> endSyntax Example:
define square (n) right(n) down(n) left(n) up(n) endwhich can later be invoked, such as
square(15)
This allows you to define your own subroutine, as discussed on pp. 250-255 of the Dale/Lewis text. If your subroutines accepts one or more parameters, the parameter names must be specified. If more than one paramter is expected the names should be separated by commas. Similar syntax is expected when invoking the procedure from elsewhere.
Parameters are passed by value. The return value is optional
A table of relational operators is given on page 233; a similar table for Palgo is:
Relationship | Symbol |
---|---|
equal to | = = |
not equal to | != |
less than or equal to | <= |
greater than or equal to | >= |
less than | < |
greater than | > |
Palgo uses the following symbols for common logical operators.
Operator | Symbol |
---|---|
AND | && |
OR | || |
NOT | ! |
For example, one might write:
if (x<5) && ((x!=y) || (y>=2)) then
...
end
Operator | Symbol |
---|---|
addition | + |
subtraction | - |
multiplication | * |
division (quotient) | / |
division (remainder) | % |
Please note the behavior of division on integers. Specifically, since 23/4 would result in a quotient of five with a remainder of three, you will find that the expression "23/4" has value 5, and the expression "23%4" has value 3.
Last modified: 18 February 2003