In class, we worked through this sequence of questions in random groups at the whiteboards.
If you were away
You need no computer for this. Work through the questions below in order, before looking at any answers. Each question builds on the one before it. Surprised by what you discover in a given question? That is where the learning happens.
Once you have an answer, expand the answer box to check your work. If a question didn’t go the way you expected, try to explain why before moving on.
When you reach the end, read the summary of key ideas that we built together in class.
Part 1: which one doesn’t belong?
Copy this grid onto your board.
7
"7"
7.5
false
Question 1
Each of you picks the one that doesn’t belong, and gives a reason. There is more than one right answer.
Answer
Any corner, as long as you have a reason. For example: false is the only one that is not a number.
Question 2
Find a reason for every corner. Write the reason beside it.
Answer
7 is the only whole number.
"7" is the only one in quotation marks, so it is the only one that is text.
7.5 is the only one with a decimal point.
false is the only one that is not a number or text. It is the answer to a yes-or-no question.
Question 3
7 and "7" look almost the same. Would 7 + 7 and "7" + "7" give the same answer? What do you think a computer does with each?
Answer
No. 7 + 7 is 14: two numbers are added. "7" + "7" is "77": the quotation marks make them text, and text is joined end to end, not added.
Question 4
Sort the four values into kinds: a whole number, a number with a decimal point, some text, a yes-or-no answer. Then add one new value of your own to each kind.
Answer
Whole number: 7, and for example 42 or -3. Decimal: 7.5, and for example 0.99. Text: "7", and for example "Guess". Yes-or-no: false, and true is the only other one.
Swift has a name for each kind: Int, Double, String, and Bool.
Question 5
Where in a game would you use each kind? Give one example for each.
Answer
A score or a number of lives is a whole number. A price, or the seconds left on a timer, is a decimal. A player’s name, or a message, is text. “Is the game over?” is a yes-or-no answer. Your examples may differ.
Part 2: an AP exam question
The AP exam writes code in its own way. The arrow ← means work out the value on the right, then store it in the name on the left. DISPLAY shows a value on the screen; in Swift, print does the same job.
Question 6
Read this line aloud, then say in your own words what it does.
a ← 5
Answer
“a gets 5”, or “store 5 in a”. The name a now holds the value 5. A line that stores a value in a name is an assignment. In Swift, the same line is a = 5, and = is the assignment operator.
Question 7
Now trace these two lines. What does b hold at the end? Is b linked to a from now on, or does b have its own copy?
a ← 5
b ← a
Answer
b holds 5. It has its own copy. The second line copies the value in a into b once, and that is all. Nothing links them afterwards.
Question 8
Here is the next line. Does it make sense as a math equation? What does the computer do with it?
a ← a + 3
Answer
As math it is nonsense: nothing equals itself plus 3. But the arrow is not “equals”. The computer works out the right side first, 5 + 3 = 8, then stores 8 in a. The old 5 is gone.
Question 9
Put it all together. What does this code display?
a ← 5
b ← a
a ← a + 3
DISPLAY(b)
DISPLAY(a)
(a) 5 8
(b) 8 8
(c) 5 5
(d) 8 5
Answer
(a)5 8.
Line
a
b
a ← 5
5
b ← a
5
5
a ← a + 3
8
5
Question 10
Someone chose (b). What did they think was happening? Someone else chose (c). What did they think?
Answer
(b) thinks b stays linked to a, so it changes when a does. It does not: b ← a copied the value once. (c) thinks a ← a + 3 does not change a. It does: the right side is worked out, then stored back in a. (d) just mixes up which value is displayed first.
Question 11
Write all five lines in Swift. Use let or var, give each name its type, and use print where the AP code says DISPLAY.
Answer
var a: Int = 5let b: Int = aa = a + 3print(b)print(a)
a must be a var, because the third line changes it. b never changes, so it can be a let. Both hold whole numbers, so both are Int. print is how a Swift program shows a value in a command-line program: it is Swift’s DISPLAY.
Bonus
Predict: what does a computer answer for 7 / 2? Is it 3.5, or something else? Write your prediction down. You test it for real later today.
Answer
In Swift, 7 / 2 is 3. Two whole numbers divided give a whole number, and the .5 is dropped, not rounded. 7.0 / 2 is 3.5. You will see this in Variables and Data Types.
Summary of key ideas
Here is the summary of the concepts we consolidated together in class, after the whiteboard work:
If the whiteboard conversation didn’t quite click, or you were away, this is the document to read for a concrete summary of the key ideas: the four kinds of value and their Swift names, how to tell them apart by how they are written, what the arrow and = really do, and how to trace a few lines of code with a table.
Curriculum connection
A1.1
use constants and variables, including integers, floating points, strings, and Boolean values, correctly in computer programs;
AAP-1.A.1 A variable is an abstraction inside a program that can hold a value. Each variable has associated data storage that represents one value at a time, but that value can be a list or other collection that in turn contains multiple values.
AAP-1.A.2 Using meaningful variable names helps with the readability of program code and understanding of what values are represented by the variables.
AAP-1.A.3 Some programming languages provide types to represent data, which are referenced using variables. These types include numbers, Booleans, lists, and strings.
AAP-1.A.4 Some values are better suited to representation using one type of datum rather than another.