Background
jLogo Programming
- Commanding a Turtle
- Pseudocode
- Adding New Commands
- Iteration & Animation
- Hierarchical Structure
- Procedure Inputs
- Operators & Expressions
- Defining Operators
- Words & Sentences
- User Interface Events
- What If? (Predicates)
- Recursion
- Local Variables
- Global Variables
- Word/Sentence Iteration
- Mastermind Project
- Turtles As Actors
- Arrays
- File Input/Output
Java
- A Java Program
- What's a Class?
- Extending Existing Classes
- Types
- Turtle Graphics
- Control Flow
- User Interface Events
Appendices
- Jargon
- What Is TG?
- TG Directives
- jLogo Primitives
- TG Editor
- Java Tables
- Example Programs
- *** New ***:
Installation Notes
Updates
- December 13, 2008
- January 6, 2012
- March 15, 2013
- January 20, 2014
- February 13, 2014
- July 29, 2014
- January 18, 2016
- January 29, 2016
- August 19, 2016
Lastly
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.
|
This procedure can also be entered into the Editor spanning multiple lines with continuation characters (tilde) and indented for readability.
|
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.
|
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.