Additional Notes on Painting with Palgo
(provided by your instructor)

You may Start the Palgo applet if you are already familiar with it. Otherwise, you may wish to read these notes first.

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
  • A Palgo User's Guide
  • The (imaginary) Pen
  • Command Syntax and Semantics
  • commands that effect the drawing surface or the pen
  • numcells(<num>)
  • clear()
  • color(<name>)
  • color(<r>,<g>,<b>)
  • goto(<x>,<y>)
  • draw(<x>,<y>)
  • pen("down")
  • pen("up")
  • left(<num>)
  • right(<num>)
  • up(<num>)
  • down(<num>)
  • commands for input/output
  • print(<expression>)
  • <variable> = input_number()
  • <variable> = input()
  • other commands
  • wait(<ms>)
  • random(<num>)
  • /* comment */
  • high-level programming constructs
  • <variable> = <expression>
  • if <boolean expression> then
  • if <boolean expression> then ... else
  • repeat <num> times
  • for <variable> = <numA> to <numB>
  • while <boolean expression>
  • define <subroutinename> (<list of parameter names>)
  • Defining boolean expressions
  • Defining arithmetic expressions

  • 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.


    A Palgo User's Guide

    Palgo is an environment for drawing. The drawing area includes 400 pixels arranged in a 20x20 grid, with the top-left corner indexed as (0,0).

    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 current color of the pen
  • The current location of the pen (x- and y- coordinates)
  • The position of the pen is either "up" or "down."
    When the pen is down, it will automatically color squares as the pen moves. When the pen is up, it will not color squares unless you explicitly command that it "draw" the current square.
  • When the applet initially starts, the pen is in the "down" position, with color red, and position (0,0). However, these defaults are modified as programs run, and so at the beginning of a new program you should always explicitly set the state as you desire.

    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.


    Command Syntax and Semantics

    All commands, as stated below, involve parentheses, and the opening parenthesis '(' must come immediately after the command, with no intervening space. If you fail to follow this requirement, your program may just freeze or generate something strange.

    Example:

     draw(x, y)   /* legal */
     draw (x, y)   /* wrong */
    

  • commands that effect the drawing surface or the pen

  • numcells(<num>)
    Syntax Example: numcells(40)

    This resets the grid to have "num" rows and "num" columns (the default grid is 20x20).
    Notes:

  • If you call it without specifying a parameter, e.g. numcells() the function returns the current size of the grid.
  • clear()

    This resets all squares back to the background color (white), effectively clearing the screen.

  • color(<name>)
    Syntax Example: color("red")

    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:

  • color is a built in function; it is not a variable name. Therefore, you should not try to set it with an assignment statement (such as color = "red"). Nor should you try to use it in a expression (such as if color=="red" then).

  • Recognized names are: red, blue, green, yellow, black, white, gray, orange, pink, cyan and magenta.
  • color(<r>,<g>,<b>)
    Syntax Example: color(100,120,55)

    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].

  • goto(<x>,<y>)
    Syntax Example: goto(18,7)

    This jumps the pen location to the specified location, however it does not cause any squares to be drawn.

  • draw(<x>,<y>)
    This jumps the pen location to the specified location, as with goto, however it also draws that square using the current pen color.
    Syntax Example: draw(18,7)
    Syntax Example: draw()
    Notes:
  • If you do not specify the x-value and y-value, then the pen does not move, however the current location will be drawn.
  • If you specify an x,y pair which is not on the grid, an error message will be printed.
  • CAUTION: There must not be a space before the '('.
  • pen("down")
    This command places the point of the imaginary pen down against the imaginary paper. It does not immediately color the current square, but if you leave the pen set to the down position and then reposition the pen using the (left/right/up/down) commands described below, squares will be colored as the pen moves away from them.
    Notes:
  • The parameter "down" must be in quotes.
  • pen("up")
    This command lifts the point of the imaginary pen up away from the imaginary paper. After this command, no coloring will take place unless you explicitly "draw" a sqaure or unless you later place the pen point back down.

  • left(<num>)
    Syntax Example: left()
    Syntax Example: left(8)

    This has the effect of moving the current location of the pen the specified number of squares to the left.
    Notes:

  • If the pen position is currently "down" this will have the effect of coloring squares as the pen moves passed, though it will not color the last location reached, which becomes the current location.
  • If no number is given, the default is to move 1 square
  • If your command would result in moving past the left side of the grid, the pen "wraps" around to the far right side of the same row.
  • right(<num>)
    up(<num>)
    down(<num>)
    Defined similarly to the "left" command.
  • commands for input/output

  • print(<expression>)
    Syntax Example: print("hello")
    Syntax Example: print(i)

    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:

  • The applet has a flaw. If you try to include the character ":" within a string you are printing, the command will not work.
  • <variable> = input_number()
    Syntax Example: size = input_number()

    This reads a numeric value from the user and stores it in the specified variable.

  • <variable> = input()
    Syntax Example: usercolor = input()

    This reads a character string from the user and stores it in the specified variable.
    Notes:

  • Please note that the parentheses are required, even though there is no parameter between them.
  • other commands

  • wait(<ms>)
    Syntax Example: wait(1000)

    This causes a delay of (approximately) the specified number of milliseconds.

  • random(<num>)
    Syntax Example: k = random(25)

    This sets the variable to a randomly chosen integer in the range [0,num-1] for the given parameter num.

  • /* comment */
    Syntax Example: /* this is ignored */

    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.

  • high-level programming constructs

  • <variable> = <expression>
    Syntax Example: favoritecolor = "blue"
    Syntax Example: mycount = 5
    Syntax Example: mycount = mycount+1

    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
      ...
    end
    
    Syntax Example:
      if count==5 then
        right();
      end
    
    The 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.
    Note:
  • You must remember to include the keyword then in its proper place or the behavior will be unexpected.
  • if <boolean expression> then
      ...
    else
      ...
    end
    
    Syntax Example:
      if count==5 then
        right();
      else
        up();
      end
    
    The 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
      ...
    end
    
    Syntax Example:
      repeat 8 times
        down(1)
        right(1)
      end
    
    The 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>
      ...
    end
    
    Syntax Example:
      for i = 5 to 10
        draw(19,i)
      end
    
    Similar 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>
      ...
    end
    
    Syntax Example:
      while i>10
        down(1)
        i = i/2
      end
    
    The 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>
    end
    
    Syntax Example:
      define square (n)
         right(n)
         down(n)
         left(n)
         up(n)
      end
    
    which 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

  • Defining boolean expressions

    Boolean expressions are discussed starting on pp 232-233 of the Dale/Lewis text.

    A table of relational operators is given on page 233; a similar table for Palgo is:

    RelationshipSymbol
    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.

    OperatorSymbol
    AND&&
    OR||
    NOT!

    For example, one might write:

      if (x<5) && ((x!=y) || (y>=2)) then
        ...
      end
    

  • Defining arithmetic expressions

    OperatorSymbol
    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.


  • Start the Palgo applet


    Last modified: 18 February 2003