Skip to content
Matthew edited this page Nov 11, 2024 · 4 revisions

✨ Sparkle: An Overview

The language used in the AP Computer Science Exam, now actually a programming language!

✖️ 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 <-- 13

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

💡 Logical Operators:

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

🆚 Comparison Operators:

In this language:
0 is FALSE
1 is TRUE

The supported comparison operators are:

  1. <       4. >=
  2. <=    5. ==
  3. >       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 {1} ELSE {0}

🔄 For Loops

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
a

In the example above, a would now have the value of 15

🔁 While Loops

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

📖 Resources

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

Writen with the help of a tutorial by CodePulse, which can be found here: https://youtu.be/Eythq9848Fg?si=yDuSPkEzG2y1X1cm

Clone this wiki locally