DISPLAY("Why did the chicken cross the road?") userResponse ← INPUT()
INPUT()accepts the value from the user and returns that input value, which is then assigned to the variable
userResponsewith the
←syntax. In Snap!, this is just like how
answer
accepts a value from the user and reports it, and that report is what the computer set
s the variable user response to.score ← score + 1or .
costumes[2]or . All three versions of this code would report/return the penguin costume (if only the AP language had costumes as a data type):
[Señora, Ms. C, my cat, Hannah, Jake]. The items are positioned in the list in the order they appear in the text: "Señora" has index 1, "Ms. C" has index 2, and so on.
shoppingList ← []or .
shoppingList ← [apples, bread, carrots, rice, pasta]or . (In this app, you'll want the
shopping list
to begin empty, and then the user will add
or insert
additional grocery items one at a time.)INSERT(shoppingList, 2, "tomatoes")or .
APPEND(shoppingList, "tomatoes")or .
REMOVE(shoppingList, 2)or .
item of
anywhere you can use any other expression. For example:
myFavoriteFood ← shoppingList[3]or
shoppingList[2] ← yourFavoriteFoodor .
shoppingList[1] ← shoppingList[3]or .
When you run this script in Snap!, the second line of code assigns to shopping list 2 the value of shopping list (that is, the same list, not a copy). So, the third line of code modifies both variables:
shoppingList2 ← shoppingListmakes a copy of the list. So modifying one of them does not modify the other.
The rules for how to use lists and how they behave differ depending on the programming language you are using.
goto()isn't built in to the AP's language so it is written in lower case like other programmer-defined procedures.
FOR EACH point IN myDrawing { goto(point) }
=, >, <, ≥, ≤as well as a sixth:
≠, which means "not-equal" and reports
false
if the two inputs are equal and otherwise reports true
(if they are not equal). When you write the block, it will work like this:These six relational operators all report a Boolean value (true
or false
).
AND,
OR, and
NOTand will work exactly the same way as they do in Snap!.
mod
operator: The expression would be written as 17 MOD 5on the exam. If you see an expression with variables as input to
mod
, such as a MOD b, you can assume that a is zero or positive, and b is strictly positive (because you can't divide by 0).
+,
-,
*,
/(plus, minus, times, divide) as well as
MOD. Arithmetic operators are part of most programming languages. (Most text languages use
*rather than
×for multiplication because
×isn't on most keyboards, and because it looks too much like the letter x.)
+
block is an input to the ×
block, so the expression means 3×(5+4). Similarly, means (3×5)+4. In Snap!, it's as if there are parentheses around every operation. But in text languages, you can write 3 * 4 + 5without parentheses, so they need the rules you learn in math class (multiplication before addition, and so on). The
mod
operator is like multiplication and division; it happens before addition and subtraction. So for example, 7 + 5 MOD 2 - 6means
7 + 1 - 6, which is, of course, 2.
move()and
turn_clockwise()aren't built in to the AP's language so they are written in lower case like other programmer-defined procedures.
IF(size > 15) { REPEAT 4 TIMES { move(size) turn_clockwise(90) } }
As in Snap!, if the condition (
size) > 15
is true
, the code inside the if
statement runs; if the condition is false
, the code does not run.
RETURN(like
report
). For example, this definitionIn many languages (including Snap!) variables must be declared in some way (in Snap!, you can click the "Make a variable" button or use the script variables
block). But the AP's language doesn't include variable declarations, so you won't see them on the exam.
PROCEDURE squareRoots(number) { positiveRoot ← sqrt(number) RETURN([positiveRoot, -1 * positiveRoot]) }
report
in Snap!, when a RETURNstatement is executed, the flow of control returns to the place in your code where the procedure was called, and the procedure returns the value of the expression inside the
RETURNcommand or
report
block.sqrtisn't built in to the AP's language so it is written in lower case like other programmer-defined procedures.
IF(a > b) { RETURN(true) } ELSE { RETURN(a = b) }
a > bis true, the code in first block of statements runs; if it is false, the code in second block of statements runs.