To do: October 28
Recall patterns in making event-driven apps
Moving forward
On the whiteboard: what kind of situations will an app need to keep track of information?
Today’s tasks
Variables Explore
Warm Up
Do This:
Warm Up
Samples of apps using variables
Warm Up
Prompt:
These are samples of the kinds of apps you'll be able to build by the end of this unit.
Write down in today's notes in your IN at 1-2 examples where each app is keeping track of a piece of information or using it to make a decision.
“hi”
“hi there”
“c u l8r”
Strings
Sequence of any characters
Inside double quotes
22
9
548
Numbers
Made of the digits 0...9
No quotes
Do This:
4. Write an example of a number.
5. Write an example of a string.
Value
One piece of information or data
“123”
123
Unit 4 Lesson 1 - Activity
2
3
Operators
Fancy name for + - * /
Expression
Combination of operators and values
Evaluates to single value
+
5
evaluates to
Activity
"Card"
"inals"
Operator + with string(s) means to concatenate
Expression
Combination of string, operator +, and string/number evaluates to single string
+
evaluates to
Activity
"Cardinals"
"Card"
"inals"
Operator + with string(s) means to concatenate
Expression
Combination of string, operator +, and string/number evaluates to single string
+
"Cardinals"
evaluates to
string + number -> string
"Bell"
1
+
Activity
->
12
"dog"
+
->
number + string -> string
6.
7.
"Card"
"inals"
Operator + with string(s) means to concatenate
Expression
Combination of string, operator +, and string/number evaluates to single string
+
"Cardinals"
evaluates to
string + number -> string
"Bell"
1
+
Activity
"Bell1"
->
12
"dog"
+
"12dog"
->
number + string -> string
6.
7.
3
4
evaluates to
+
7
5
2
evaluates to
-
3
“for”
“ever”
evaluates to
+
“forever”
“gr”
8
evaluates to
+
“gr8”
2
“day”
evaluates to
+
“2day”
11
2
evaluates to
*
22
10
2
evaluates to
/
5
If you’re using one or two strings, you can only use the + operator.
The other operators don’t make sense with strings.
Activity
3
4
evaluates to
+
7
5
2
evaluates to
-
3
“for”
“ever”
evaluates to
+
“forever”
“gr”
8
evaluates to
+
“gr8”
2
“day”
evaluates to
+
“2day”
Do This: Evaluate these expressions. Remember that strings use quotes.
4
5
+
10
9
-
“tree”
“house”
+
“you”
“r”
+
3
“D”
+
11
2
evaluates to
*
22
10
2
evaluates to
/
5
Activity
“1”
“2”
+
8.
9.
10.
11.
12
13.
3
4
evaluates to
+
7
5
2
evaluates to
-
3
“for”
“ever”
evaluates to
+
“forever”
“gr”
8
evaluates to
+
“gr8”
2
“day”
evaluates to
+
“2day”
Do This: Evaluate these expressions. Pay attention to what color stickies you create and if you use quotes.
4
5
+
10
9
-
“tree”
“house”
+
“you”
“r”
+
3
“D”
+
11
2
evaluates to
*
22
10
2
evaluates to
/
5
If you’re using one or two strings, you can only use the + operator. The others don’t make sense!
Activity
9
1
"treehouse"
“your”
“3D”
“1”
“2”
“12”
+
8.
9.
10.
11.
12
13.
->
->
->
->
->
->
word
2
num
“tree”
Variables
Unit 4 Lesson 1 - Activity
word
2
num
“tree”
Variables
←
The left arrow is the “Assignment operator”
“Assign” means to put a value inside the variable.
Do This:
14. Draw a model of both of these variables below.
Unit 4 Lesson 1 - Activity
word
2
num
“tree”
Variables
←
The left arrow is the “Assignment operator”
“Assign” means to put a value inside the variable.
num <- 2
word <- "tree"
Unit 4 Lesson 1 - Activity
word
2
num
“tree”
15. What’s the result?
num <- 2
num <- num + 3
word <- "tree"
word <- word + "house"
Unit 4 Lesson 1 - Activity
word
5
num
“treehouse”
num <- num + 3
num + 3 evaluates to 2 + 3 so
num <- 5
word <- word + "house"
word + "house" evaluates to "tree" + "house" so
word <- "treehouse"
Unit 4 Lesson 1 - Activity
Let’s start writing programs �that control our variables.
Unit 4 Lesson 1 - Activity
num
var
Creates a new variable
00 var num
Command to create variable
Variable’s name
Line number
Unit 4 Lesson 1 - Activity
←
“Assignment operator”
“Assign”: put a value inside the variable.
Variables can only hold one value. If there’s already a value in there, it gets "thrown away."
“num gets 3” and “num gets 5”
00 var num�
01 num ← 3�
02 num ← 5
num
num
num
3
5
3
Do This: Run this program
Unit 4 Lesson 1 - Activity
In pseudocode (used on the AP Exam), the assignment operator is written as:
←
In JavaScript, the assignment operator �is written as
=
Unit 4 Lesson 1 - Activity
So the command
num ← num + 1
Can be written as
num = num + 1
To assign a variable means:
To put a value inside a variable �
Unit 4 Lesson 1 - Activity
Assign a Variable with Expression
Evaluate the expression first to get one value.
Assign the value as normal
00 var num
01 num ← 1 + 2
02 num ← 3 + 4�
num
num
num
3
7
3
Evaluate expression first
Unit 4 Lesson 1 - Activity
Exercise 1
Run this program. What is stored in the variables num and str at the end of the program?
00 var num
01 var str
02 str ← "to" + "day"
03 num ← 4 - 1
04 str ← str + str
05 num ← 4 * num
Unit 4 Lesson 1 - Activity
00 var num1
01 num1 ← 1
02 var num2
03 num2 ← num1 + 1�04 num1 ← 5
Exercise 2: What is stored in num1 and num2 at the end of the program?
Note: Variables aren’t “connected”.
Unit 4 Lesson 1 - Activity
00 var num
01 var str
02 num ← 5
03 str ← "Friday" + "!"
04 num ← num + 1
05 str ← "It’s " + str
06 num ← num * 2
07 num ← num + 1
08 num ← num * 2
Unit 4 Lesson 1 - Activity
Exercise 3: Run this program. What is stored in variables num and str at the end of the program?
pow
pow
pow
Key Takeaways
= or ←
“hi”
22
10
2
evaluates to
/
5
00 var pow
01 pow ← 1 + 2
02 pow ← 3 + 4�
3
7
3
Unit 4 Lesson 1 - Activity
Homework