-
Notifications
You must be signed in to change notification settings - Fork 1
Spindle: Language Documentation
The language used in the AP CSP Exam, now a programming language!
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
How would you swap the values of two variables?
a <-- 10
b <-- 10
c <-- a
a <-- b
b <-- c
NOT: Negates the condition
AND: Returns TRUE if and only if BOTH conditions are TRUE
OR: Returns TRUE if at least one of the conditions are true
In this language:
0 is FALSE
1 is TRUE
The supported comparison operators are:
- < 4. >=
- <= 5. ==
- > 6. !=
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 {
RETURN 1
} ELSE {
RETURN 0
}
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)
In the example above, a would now have the value of 15
While loops are defined by the keywords REPEAT UNTIL, then the condition [surrounded by ()], and then the code you want repeated [surrounded by {}] Like the code reads, the loop will run until the condition is true. You can think it as a while loop running while (NOT condition)
a <-- 10
REPEAT UNTIL (a > 30) { a <-- a + 4}
In the example above, a would now have the value of 34
Functions are defined by the keyword 'PROCEDURE', then the function name and or arguments [surrounded by ()], and then the code you want in the function body [surrounded by {}]
PROCEDURE SAY_HI(excitement) {
REPEAT excitement TIMES {
DISPLAY("Hi!")
}
}
*The DISPLAY() function is an built-in function, meaning it doesn't have to be defined. Other built-in functions are: INPUT(): Gets user input CLEAR(): Clears the console IS_NUM(something): Returns True (1) if something is a number and False (0) otherwise IS_STR(something): Returns True (1) if something is a string and False (0) otherwise IS_LIST(something): Returns True (1) if something is a list and False (0) otherwise LENGTH(list): Returns the length of a list. As per college board guidelines, starts counting at 1 RUN(filename): Runs a spindle file at a specific filename. You shouldn't use this directly in your programs
The AP Computer Science Exam Reference Sheet can be found here: https://apcentral.collegeboard.org/media/pdf/ap-computer-science-principles-exam-reference-sheet.pdf
Written with the help of a tutorial by CodePulse, which can be found here: https://youtu.be/Eythq9848Fg?si=yDuSPkEzG2y1X1cm