BFOIT - Introduction to Computer Programming

MousedQuadrants Program

I picked this little exercise because it demonstrates where the AND operator helps us write programs that are easier to understand.  To show you why, here is a solution, a program that does what we want but does not use AND.

   to mouseClicked
     if greater? mousex 0  [if greater? mousey 0 [println [Quadrant I]]]   
     if greater? mousex 0 [if less? mousey 0 [println [Quadrant IV]]]
     if less? mousex 0 [if greater? mousey 0 [println [Quadrant II]]]
     if less? mousex 0  [if less? mousey 0 [println [Quadrant III]]]

This procedure can also be entered into the Editor spanning multiple lines with continuation characters (tilde) and indented for readability.

   to mouseClicked
     if greater? mousex 0         ~
        [if greater? mousey 0     ~
            [println [Quadrant I]]]
     if greater? mousex 0          ~
        [if less? mousey 0         ~
            [println [Quadrant IV]]]
     if less? mousex 0             ~
        [if greater? mousey 0      ~
            [println [Quadrant II]]]
     if less? mousex 0              ~
        [if less? mousey 0          ~
            [println [Quadrant III]]]   

What we have here is nested IF commands.  We have an IF instruction inside the second input of an IF command, the instruction list that is performed when the first input (the predicate) is true. 

Here is a plumbing diagram for the first IF command.

It has been a few lessons since plumbing diagrams were introduced.  Here we actually have three plumbing diagrams.  The way to read this illustration is to start in the lower-left corner.  Here we have the outermost IF instruction.  Its input is a predicate - true is output if the mouse's X coordinate is greater than zero, otherwise false is output.  The second input is a list that represents instructions that will be performed if (and only if) the first input outputs true.

The second plumbing diagram, above the first's second input, represents the instruction contained in the outermost IF's second input.  There are two differences between this plumbing diagram and the first one: (1) the predicate input here deals with the mouse's Y coordinate instead of its X coordinate, and (2) the second input, a list, is a simpler instruction.

Finally, the third plumbing diagram is in the top-right corner, above the second input of the IF instruction below it.  It is a simple PRINTLN instruction with a single input - something to display in the CommandCenter.

Given the AND operator, we do not need to nest IF instructions.  In the following version of mouseClicked, I use an AND operator to combine the outputs of the two predicate expressions.  Notice that this code has one less set of square brackets.  It's so easy to forget the trailing square bracket, especially when they are nested.  I have added (surrounded) both of the predicates with parenthesis so that it is easier to recognize the two inputs for AND.

   to mouseclicked
     if and (greater? mousex 0) (greater? mousey 0) [println [Quadrant I]]   
     if and (less? mousex 0) (greater? mousey 0) [println [Quadrant II]]
     if and (less? mousex 0) (less? mousey 0) [println [Quadrant III]]
     if and (greater? mousex 0) (less? mousey 0) [println [Quadrant IV]]

Hopefully this version of mouseClicked makes sense to you and you agree that its code reflects what we want.

The following illustration has one less plumbing diagram than the one above.  It has one IF instruction instead of two.  In this case we have only one predicate, one that's a bit more complex, but aids in simplifying the big picture.

Hopefully this shows you how the use of an AND operator can make your programs easier to read, easier to understand.

Public Domain Mark
This work (BFOIT: Introduction to Computer Programming, by Guy M. Haas),
identified by Berkeley Foundation for Opportunities in IT (BFOIT),
is free of known copyright restrictions.