Pro Tip: Spindle's syntax mirrors what you'll see on the AP CSP exam, helping you feel confident on test day.
Programming Guide
data_object Variables
Varibles can be assigned a value using "<--" OR "<-" For example, the following code sets a to 10 and then to 13 (a's value plus 3).
a <- 10
a <-- [1,2,3]
Varibles can be assigned to other varibles!
b <-- a / 2
If you added this line to the lines above, it would set b to a's value divided by 2, resulting in 6.5
code Logical Operators
NOT
Negates the condition
AND
Returns TRUE if both conditions are TRUE
OR
Returns TRUE if at least one condition is TRUE
🆚 Comparison Operators:
In this language:
0 is FALSE
1 is TRUE
The supported comparison operators are:
- < 4. >=
- <= 5. ==
- > 6. !=
❔ If Statements:
If Statements can be defined with the keyword IF, and then the expression that must be true. The code that is to be ran when the the condition is met must be wrapred in brackets. If the condition for the IF statement is false, you can write an ELSE statement that will be ran instead
a <-- 5
IF a==5 {
DISPLAY(1)
} ELSE {
DISPLAY(0)
}
loop Loops
For Loops (REPEAT TIMES)
For Loops are defined by the keyword REPEAT, followed by the number of times you want it to repeat, the keyword TIMES, and finally the code you want to be repeated.
a <-- 5
REPEAT 10 TIMES {
a <-- a + 1
}
DISPLAY(a) # Output: 15
While Loops (REPEAT UNTIL)
While loops use REPEAT UNTIL, followed by a condition in parentheses, and the code block in curly braces. The loop continues until the condition becomes true.
a <-- 10
REPEAT UNTIL (a > 30) {
a <-- a + 4
} # Final value: 34
function Functions
Procedures (Functions)
Functions are defined using the keyword 'PROCEDURE', followed by the function name and arguments in parentheses, with the code block in curly braces.
PROCEDURE SAY_HI(excitement) {
REPEAT excitement TIMES {
DISPLAY("Hi!")
}
}
SAY_HI(3) # Outputs: Hi! Hi! Hi!
Built-in Functions
DISPLAY(value)
DISPLAY(value): Outputs the value to the console. You can display numbers, strings, lists, or the result of an expression.
INPUT()
INPUT(): Prompts the user for input and returns the value entered. Useful for interactive programs.
IS_NUM(value)
IS_NUM(value): Returns 1 if the value is a number, 0 otherwise. Useful for checking data types before performing calculations.
IS_STR(value)
IS_STR(value): Returns 1 if the value is a string, 0 otherwise. Use this to check if a variable contains text.
IS_LIST(value)
IS_LIST(value): Returns 1 if the value is a list, 0 otherwise. Use this to check if a variable contains a list/array.
IS_FUNCTION(value)
IS_FUNCTION(value): Returns 1 if the value is a function/procedure, 0 otherwise. Useful for advanced programming and debugging.
APPEND(list, value)
APPEND(list, value): Adds the value to the end of the list. Use this to grow a list dynamically.
POP(list, index)
POP(list, index): Removes and returns the element at the specified index from the list. Indexing starts at 1.
EXTEND(listA, listB)
EXTEND(listA, listB): Adds all elements from listB to the end of listA. Useful for combining lists.
LENGTH(list)
LENGTH(list): Returns the number of elements in the list. Note: List indexing starts at 1, following College Board conventions.
CLEAR()
CLEAR(): Clears the console. Note: This function is only available in the Native IDE.
RUN("filename")
RUN("filename"): Runs another Spindle file by filename. Note: This function is only available in the Native IDE.
Robot IDE Built-in Functions
MOVE_FORWARD()
: Moves the robot forward by one spaceROTATE_LEFT()
: Rotates the robot 90 degrees to the leftROTATE_RIGHT()
: Rotates the robot 90 degrees to the rightCAN_MOVE(direction)
: Returns 1 if the robot can move in the specified direction, 0 otherwise
# Example: Move forward if possible, then turn left
IF CAN_MOVE("NORTH") {
MOVE_FORWARD()
}
ROTATE_LEFT()
MOVE_FORWARD()