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
- Turtles As Actors
- Arrays
Java
- A Java Program
- What's a Class?
- Extending Existing Classes
- Types
- Turtle Graphics
- Control Flow
- User Interface Events
Appendices
Updates
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 |
| ARRAYTOLIST | array | Outputs a list whose members are the members of the input array. | MAKE "xxx ARRAYTOLIST :ary |
| 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 |
| 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 |
| FPUT | thing list |
Outputs a list equal to its second input with one extra member, thing, at the beginning. If the second input is a word, then the first input must be a one-letter word, and FPUT is equivalent to WORD. | |
| 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 |
| LIST | thing1 thing2 |
Outputs a list whose members are its inputs, which can be any jLogo datum (arrays, lists, and words). | |
| LISTTOARRAY | list | Outputs an array whose elements are the elements of the input list. | |
| LOWERCASE | word | Outputs a copy of the input word, but with all uppercase letters changed to the corresponding lowercase letters. | |
| LPUT | thing list |
Outputs a list equal to its second input with one extra member, the first input, at the end. If the second input is a word, then the first input must be a one-letter word, and the output: | |
| 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 | |
| SENTENCE SE |
wordOrSentence wordOrSentence |
Outputs a sentence consisting of its first input concatenated with its second input. | SENTENCE "word :sent |
| THING | name | Outputs the value in its input, a global variable named name. Dots name (":NAME") can be thought of as an abbreviation for THING "NAME. But, the real need for THING is when the names of global variables are constructed dynamically. | THING :curSequence |
| UPPERCASE | word | Outputs a copy of the input word, but with all lowercase letters changed to the corresponding uppercase letters. | |
| 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 |
| EVALUATE EVAL |
operation | The input operator, which must be a word or list, is performed and the output is the output of EVALUATE. This provides a mechanism for a program to dynamically construct an operation. | |
| 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 |
| REPCOUNT | Outputs the current value of the variable controlling iteration in the REPEAT command in which it is invoked. | REPEAT 20 [PRINTLN REPCOUNT] |
|
| 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 element in rangeSentence and moves toward the second element in rangeSentence in increments as specified by the third element in rangeSentence. | REPEAT [-2 2 .5] [PRINTLN 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 |
| ARCTAN | number | Outputs the arc tangent of number in degrees. | |
| ASCII | character | Outputs the integer (between 0 and 127) that represents the character in the ASCII code. | ASCII "A |
| ASHIFT | num1 num2 |
Outputs num1 arithmetic-shifted to the left by num2 bits. If num2 is negative, the shift is to the right with sign extension. The inputs must be integers. | |
| BITAND | num1 num2 |
Outputs the bitwise AND of its inputs, which must be integers. | |
| BITNOT | number | Outputs the bitwise NOT of its input, which must be an integer. | |
| BITOR | num1 num2 |
Outputs the bitwise OR of its inputs, which must be integers. | |
| BITXOR | num1 num2 |
Outputs the bitwise exclusive OR of its inputs, which must be integers. | |
| 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 |
| INT | number | Outputs number with its fractional part removed. | |
| LSHIFT | num1 num2 |
Outputs num1 shifted to the left by num2 bits. If num2 is negative, the shift is to the right with zero fill. The inputs must be integers. | |
| LOG | number | Outputs the natural logarithm of the input number. | |
| MINUS | number | Outputs the negative of number | MINUS 122 |
| POWER POW |
num1 num2 |
Outputs num1 to the num2 power. | |
| 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 | |
| TIME | Outputs the current time in milliseconds since January 1, 1970. TIME can be used to measure how long it takes to do something. |
Multi-Threading Support
| Name | Input(s) | Description | Example |
| ASK | name [operation] |
The turtle named name is passed an operation (an operator and its inputs if any) which it interprets and outputs the result to the asking turtle. | ASK "otherTurtle [POS] |
| 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 named name to perform the instructionList. | TELL "t2 [ FD 10 ] |
| WHO | Outputs the name of the current turtle. |
Predicates
| Name | Input(s) | Description | Example |
| ARRAY? ARRAYP |
thing | Outputs true if the input thing is an array, else outputs false. | |
| 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 |
| 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 |
| LIST? LISTP |
thing | Outputs true if the input thing is a list; otherwise false is output. | |
| MEMBER? MEMBERP |
characterOrWord wordOrSentence |
Outputs true if its first input is in its second input, otherwise it outputs false. | MEMBER? "e :WD |
| NUMBER? NUMBERP |
thing | Outputs true it the input thing is a number, else outputs false. | |
| PENDOWN? | Outputs true if the turtle's pen is in the down position, otherwise false. | ||
| WORD? WORDP |
thing | Outputs true it the input thing is a word (which includes numbers and boolean values). Otherwise outputs false, e.g., if thing is an array or a list. |
User-Interface Procedures
| Name | Input(s) | Description | Example | ||||||||||||||||||||
| 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 |
||||||||||||||||||||
| LOADCLIP | word clipNumber |
Reads in the .wav (waveform) audio file named word and associates it with clipNumber. This clip can then be played with the PLAYCLIP command. ClipNumber must be in the range 16 - 31. | LOADCLIP "meow.wav 16 | ||||||||||||||||||||
| 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 GraphicsCanvas is active, i.e., the mouse was previously clicked on it. |
to mouseMoved ... end |
|||||||||||||||||||||
| MOUSEPOS | Outputs the X and Y coordinates where the mouse was when it was last clicked or moved as a two member sentence. | ||||||||||||||||||||||
| 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. | ||||||||||||||||||||||
| PLAYCLIP | clipNumber |
A WAV (waveform) audio clip is played.
|
PLAYCLIP 2 | ||||||||||||||||||||
| PLAYNOTE | noteNumber length |
A MIDI note number is played for length seconds. In this scheme, middle C
is note number 60 with all other notes relative to this.
Note Numbers
Octave | C C# D D# E F F# G G# A A# B
-------+--------+--------+----+--------+--------+--------+---
-1 | 0 1 | 2 3 | 4 | 5 6 | 7 8 | 9 10 | 11
0 | 12 13 | 14 15 | 16 | 17 18 | 19 20 | 21 22 | 23
1 | 24 25 | 26 27 | 28 | 29 30 | 31 32 | 33 34 | 35
2 | 36 37 | 38 39 | 40 | 41 42 | 43 44 | 45 46 | 47
3 | 48 49 | 50 51 | 52 | 53 54 | 55 56 | 57 58 | 59
4 | 60 61 | 62 63 | 64 | 65 66 | 67 68 | 69 70 | 71
5 | 72 73 | 74 75 | 76 | 77 78 | 79 80 | 81 82 | 83
6 | 84 85 | 86 87 | 88 | 89 90 | 91 92 | 93 94 | 95
7 | 96 97 | 98 99 |100 |101 102 |103 104 |105 106 |107
8 |108 109 |110 111 |112 |113 114 |115 116 |117 118 |119
9 |120 121 |122 123 |124 |125 126 |127
The absolute octave number designations shown here are based on
Middle C = C4.
|
PLAYNOTE 60 2 | ||||||||||||||||||||
| PRINT TYPE |
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 |
| LOCALMAKE | word value |
Creates a variable with access limited to the procedure containing the LOCALMAKE command. The variable's name is word. The contents of the variable is set to value. | LOCALMAKE "rightX sum :leftX :wd |
| 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