BFOIT - Introduction to Computer Programming
Background
jLogo Programming
- Commanding a Turtle
- Pseudocode
- Adding New Commands
- Iteration & Animation
- Hierarchical Structure
- Procedure Inputs
- Primitive Operators
- Defining Operators
- Words & Sentences
- User Interface Events
- What If? (Predicates)
- Recursion
- Local Variables
- Global Variables
- Word/Sentence Iteration
- Mastermind Project
- Multiple Turtles
- Arrays
Java
- A Java Program
- What's a Class?
- Extending Existing Classes
- Types
- Turtle Graphics
- Control Flow
- User Interface Events
Appendices
Lastly
Appendix C (Primitive Procedures)
Introduction
jLogo's primitive procedures are core building blocks of the language. You may not use the names of of these primitive procedures for names of procedures you write.
- Data Manipulation
- Flow-Control Procedures
- Graphics Procedures
- Logical Procedures
- Math Procedures
- Multi-Threading
- Predicates
- User-Interface Procedures
- Variables
Data Manipulation
| Name | Input(s) | Description | Example |
| BUTFIRST BF |
wordOrSentence | If the input is a word, all characters except its first are output. If the input is a sentence, all words except the first are output. | BUTFIRST :WD |
| BUTLAST BL |
wordOrSentence | If the input is a word, all characters except its last are output. If the input is a sentence, all words except the last one are output. | BUTLAST :SENT |
| CHAR | number | outputs a single character word that is the ASCII code corresponding to the input (an integer between 0 and 127). | CHAR 65 |
| COUNT | array number wordOrSentence |
If the input is an array, the number of members it is composed of is output. If the input is a number or a word, the number of characters in it is output. If the input is a sentence, the number of words in it is output. | COUNT :WD |
| EMPTYP EMPTY? |
wordOrSentence | Outputs true if its input is a word that has no characters in it, otherwise false. Outputs true if its input is a sentence that has no words in it, otherwise false. | EMPTY? :SENT |
| FIRST | wordOrSentence | If the input is a word, its first character is output. If the input is a sentence, its first word is output. | FIRST :WD |
| ITEM | number wordOrSentence |
Outputs the number-th element of its second input. If its second input is a word, it outputs a word consisting on the number-th character. If its second input is a sentence, it outputs the number-th word of it. | ITEM 2 :SENT |
| LAST | wordOrSentence | If the input is a word, its last character is output. If the input is a sentence, its last word is output. | LAST :WD |
| MEMBERP MEMBER? |
characterOrWord wordOrSentence |
Outputs true if its first input is in its second input, otherwise it outputs false. | MEMBER? "e :WD |
| READLIST | Get a line of text input from the user (in the CommandCenter) and output it as a sentence. | MAKE "sent READLIST | |
| READWORD | Get a line of text input from the user (in the CommandCenter) and output it as a word. | MAKE "yesNo READWORD | |
| SE SENTENCE |
wordOrSentence wordOrSentence |
Outputs a sentence consisting of its first input concatenated with its second input. | SENTENCE "word :sent |
| WORD | word1 word2 |
Outputs a word consisting of its first input concatenated with its second input. | WORD "$ :word |
Flow-Control Procedures
| Name | Input(s) | Description | Example |
| IF | trueFalse instructionList |
If the trueFalse input is true, the instructionList input is performed. If trueFalse is false, nothing is done. | IF LESS? MOUSEX 0 [ ... ] |
| IFELSE | trueFalse instructionList1 instructionList2 |
If the trueFalse input is true, the instructionList1 input is performed. If trueFalse is false, instructionList2 is performed. | IFELSE PENDOWN? [ ... ] [ ... ] |
| OUTPUT | value | The current invocation of the procedure in which OUTPUT exists terminates and the value it outputs is value. | OUTPUT RANDOM 16 |
| REPEAT | count instructionList |
The instructionList input is performed the number of times specified by the count input. | REPEAT 4 [ FD 100 RT 90 ] |
| REPEAT | rangeSentence instructionList |
The instructionList input is performed while REPCOUNT is within the range of the first and second values in rangeSentence; REPCOUNT starts at the first value in rangeSentence and moves toward the second value in rangeSentence in increments as specified by the third value in rangeSentence. | REPEAT [-2 2 .5] [PR REPCOUNT] |
| RUN | sentence | Execute the sentence as if it were typed into the CommandCenter - instructions as data. | RUN :cmdsSent |
| STOP | The current invocation of the procedure in which STOP exists terminates. | IF EQUAL? :inp 0 [STOP] | |
| WAIT | number | Performing instructions is suspended for the number of milliseconds (1/1000s of a second). | WAIT 1000 |
Graphics Procedures
Logical Procedures
| Name | Input(s) | Description | Example | |||||||||||||||
| AND | trueFalse1 trueFalse2 |
Outputs true if trueFalse1 AND trueFalse2 are true, false otherwise.
|
AND true true | |||||||||||||||
| NOT | trueFalse | Outputs false if trueFalse is true, else true if trueFalse is false - the opposite trueFalse value. | NOT true | |||||||||||||||
| OR | trueFalse1 trueFalse2 |
Outputs true if trueFalse1 OR trueFalse2 is true, false otherwise.
|
OR false false |
Math Procedures
| Name | Input(s) | Description | Example |
| ABS | number | Outputs the absolute value of number | ABS -15 |
| ASCII | character | Outputs the integer (between 0 and 127) that represents the character in the ASCII code. | ASCII "A |
| CEIL | number | Outputs the smallest integer (closest to negative infinity) that is not less than number. | CEIL 44.223 |
| COS | number | Outputs the trigonometric cosine of number which is in degrees | COS 45 |
| DIFFERENCE | number1 number2 | Outputs the result of subtracting number2 from number1 | DIFFERENCE 15 7 |
| FLOOR | number | Outputs the largest integer (closest to positive infinity) that is not greater than number. | FLOOR 12.875 |
| MINUS | number | Outputs the negative of number | MINUS 122 |
| PRODUCT | number1 number2 | Outputs the result of multiplying number1 by number2 | PRODUCT 10 6 |
| QUOTIENT | number1 number2 | Outputs the result of dividing number1 by number2 | QUOTIENT 12 4 |
| RANDOM | number | Outputs some integer greater-than or equal-to zero AND less-than number | RANDOM 100 |
| REMAINDER | number1 number2 | Outputs the remainder left after dividing number1 by number2 | REMAINDER 17 3 |
| ROUND | number | Outputs the closest integer to number | ROUND 22.45 |
| SIN | number | Outputs the trigonometric sine of number which is in degrees | SIN 45 |
| SQRT | number | Outputs the square root of number | SQRT 16 |
| SUM | number1 number2 | Outputs the result of adding number1 and number2 | SUM 3 4 |
| TAN | number | Outputs the trigonometric tangent of number which is in degrees |
Multi-Threading Support
| Name | Input(s) | Description | Example |
| NEWTURTLE | name | Creates a new turtle with the specified name. The new turtle appears in the home position with its pen down. | NEWTURTLE "t2 |
| TELL | name instructionList |
Tells the turtle names name to perform the instructionList. | TELL "t2 [ FD 10 ] |
Predicates
| Name | Input(s) | Description | Example |
| EQUAL? EQUALP |
value1 value2 |
If value1 is equal to value2, EQUAL? outputs true. If not, false is output. | EQUAL? MOUSEX 0 |
| GREATER? GREATERP |
value1 value2 |
If value1 is greater than value2, GREATER? outputs true. If not, false is output. | GREATER? MOUSEX 0 |
| LESS? LESSP |
value1 value2 |
If value1 is less than value2, EQUAL? outputs true. If not, false is output. | LESS? MOUSEX 0 |
User-Interface Procedures
| Name | Input(s) | Description | Example | ||||||||||||||||||||
| CANVASHEIGHT | Outputs the current height of the graphics window in turtle steps. | ||||||||||||||||||||||
| CANVASWIDTH | Outputs the current width of the graphics window in turtle steps. | ||||||||||||||||||||||
| KEYPRESSED | keyNumber |
When a keyboard key is pressed, TG receives an event. If the key is one
that TG is interested in, a user-defined procedure with the name KEYPRESSED
(expecting one input) is invoked if it has been defined.
|
TO keyPressed :num PRINTLN WORD "key= :num END |
||||||||||||||||||||
| MOUSECLICKED | When a mouseClicked Event is received by the jLogo interpreter, it performs a user-defined procedure with the name MOUSECLICKED, if one has been defined. | ||||||||||||||||||||||
| MOUSEMOVED | When a mouseMoved Event is received by TG, it performs a user-defined procedure named mouseMoved, if one has been defined. mouseMoved Events only happen when the graphics canvas is active, i.e., the mouse was previously clicked on it. |
to mouseMoved ... end |
|||||||||||||||||||||
| MOUSEX | Outputs the X-coordinate where the mouse was when it was clicked. | ||||||||||||||||||||||
| MOUSEY | Outputs the Y-coordinate where the mouse was when it was clicked. | ||||||||||||||||||||||
| thing | Displays thing in the terminal window. | PRINT MOUSEX | |||||||||||||||||||||
| PR PRINTLN |
thing | Displays thing in the terminal window. The line is then finished off by moving the cursor to the start of the next line. | PRINTLN HEADING |
Variables
| Name | Input(s) | Description | Example |
| ARRAY | size | Outputs an array of size members, each of which is initially an empty sentence. Size must be a positive integer. | MAKE "ARY ARRAY 5 |
| GLOBAL | name | Declares a global variable named name. The variable can be referenced anywhere in the program's text following this command. The variable has no initial contents. | GLOBAL "var |
| LOCAL | name | Declares a local variable named name. The variable is local to the procedure it is in. The variable has no initial contents. | LOCAL "var |
| MAKE | name value |
Creates a variable named name if it doesn't already exist. The contents of the variable is set to value. | MAKE "WHITE 7 |
| THING | name | Outputs the value in its input, a global variable named name. ":NAME" is an abbreviation for THING "NAME. But, its real need is when the names of global variables are constructed dynamically. | THING :curSequence |
| SETITEM | index array value |
Replaces the indexth member of array with value. | SETITEM 2 :nums "two |
Go to the Table of Contents
On to TG Editor