What will you learn?

You will learn how to determine the value of a variable as a result of an assignment.

Essential Knowledge

  • The assignment operater allows a program to change the value represented to a variable.
  • The exam reference sheet uses the "⟵" to use for assignment. For example, Text: a ⟵ expression Then the code would display a block text of "a ⟵ expression". The code will evaluate expression and then assigns a copy of the result to the variable a.
  • The value stored in a variable will be the most recent value assigned. For example,

    • a ⟵ 1
    • a ⟵ b
    • a ⟵ 2

      display(b)

The code will display 1.

Storing Values

When storing values, you assign them to a variable.

Examples:

  1. highScore ⟵ 100
  • The highScore is my assignment operator and it is storing the value of 100 inside itself as a variable. This will set my highScore to 100.
  1. firstName ⟵ "Ashley"
  • The firstName is the variable and the text we are storing inside is "Ashley." We know that the name is in text because of the quotation marks around the assignment.
  1. isRaining ⟵ true
  • The isRaining variable has the intention to be a Boolean variable which checks true or false, but it's been assigned to store the value true.
  1. phoneNumber ⟵ "555-0101"
  • The phoneNumber variable and the number we are storing is "555-0101" The number is written within text because we are not planning to do math with this number.

Additional Vocabulary

Elements: individual value in a list that is assigned a unique index. And elements are referenced by an index.

Index: referencing elements in a list or string using natural numbers

Interaction

What will we do?

We will discuss to see how changing the order of the display can mess with the outcome. This interaction will be done at the end of the lesson when one of our groups team members will call up your table.

Practice Questions

Consider the following code segment:

  • num1 ⟵ 6
  • num2 ⟵ 4
  • num3 ⟵ 10

IF num1 < num2

  • num1 ⟵ num2

ELSE

  • num3 ⟵ num2

IF num2 ≥ num3

  • num1 ⟵ num2 + num3

sum ⟵ num1 + num2 + num3

What is the value of sum after the code segment is executed?

  1. 18
  2. 14
  3. 16
  4. 12
Click for the answer! 3. because the first three statements assign values to the variables. Since num1 < num2 evaluates to false, the body of the ELSE block is executed and num3 is assigned the value 4. Since num2 ≥ num3 evaluates to true, the body of the second IF block is executed and num1 is assigned the value 8. Lastly, sum is assigned the value of 8 + 4 + 4, or 16.

Consider the following code segment

  • num1 ⟵ 5
  • num2 ⟵ 10
  • num3 ⟵ 20
  • num3 ⟵ num1
  • num3 ⟵ num2
  • num1 ⟵ num3

sum ⟵ num1 + num2 + num3

insert question

  1. 35
  2. 30
  3. 45
  4. 25
Click for the answer! 3. because the value of num1 is 20 after you go through the code segment. The value of num2 is 20 as well because it is replaced by num3's value. And the value of num3 is 5 because it is replaced by num1's value. Then you add 20 + 20 + 15 and that's how you get 45.

Hacks for the lesson

Answer the practice problems and blog about which one's you got wrong. After, create at least 6 problems that are alike to what you learned in this portion of the lesson.


Consider the following code segment:

  • currentScore ⟵ 10
  • highScore ⟵ currentscore
  • currentScore ⟵ 7

DISPLAY (currentScore)

What will the currentScore be after running this code segment?

  1. 17
  2. 10
  3. 7
  4. none of the above
Click for the answer! 3. because that is the latest value that was stored within the variable.

What will the highScore be?

  1. 17
  2. 10
  3. 7
  4. none of the above
Click for the answer! 2. because the value stored in a variable will alwasy be the most recent value assigned.

Consider the following code segment:

  • num1 ⟵ 4
  • num2 ⟵ 6
  • num1 ⟵ num 2

DISPLAY(num1)

DISPLAY(num2)

What is displayed after running this code segment?

  1. 4 6
  2. 6 4
  3. 4 4
  4. 6 6
Click for the answer! 4. because the value for num 2 becomes the value of num 1 therefore making the first outcome 6 and the second outcome would be six as well because that is the value that num2 is assigned.

Consider the following code segment:

  • num1 ⟵ 25
  • num2 ⟵ 15
  • num3 ⟵ 30
  • num2 ⟵ num3
  • num3 ⟵ num1
  • num1 ⟵ num2

DISPLAY(num1)

DISPLAY(num2)

DISPLAY(num3)

What is displayed after running this code segment?

  1. 25 15 30
  2. 30 30 25
  3. 30 15 30
  4. 15 30 25
Click for the answer! 2. because for num 1 the value is replaced by num 3's value which is 30. For num 2 the value is 30 because its replaced by num 1's value which was originally 25 but then was replaced by num 3's value 30. For num 3, the value is 25 because it's value is replaced by num 2 which num 2's value was replaced by num 1's.

Consider the following code segment:

  • p ⟵ 10
  • q ⟵ 20
  • r ⟵ 30
  • s ⟵ 40
  • p ⟵ q
  • q ⟵ r
  • s ⟵ q
  • r ⟵ p

What is the value of r as a result of running this code segment

  1. 10
  2. 20
  3. 30
  4. 40
Click for the answer! 2. because q is the variable assigned to p and q's value is 20.

Consider the following code segment:

  • first ⟵ true
  • second ⟵ false
  • second ⟵ first
  • first ⟵ second

Insert Question

  1. The value of first is true, and the value of second is false.
  2. The value of first is false, and the value of second is true.
  3. The value of first is true, and the value of second is true.
  4. The value of first is false, and the value of second is false.
Click for the answer! 3. because the first two statements assign values to the variables. The third statement assigns the value of first (which is true) to second. The fourth statement assigns the value of second (which is true) to first.

Consider the following code segment:

  • a ⟵ 10
  • b ⟵ 20
  • c ⟵ 30
  • d ⟵ 40
  • x ⟵ 20
  • b ⟵ x + b
  • a ⟵ x + 1
  • d ⟵ c / d + 2

DISPLAY(a)

DISPLAY(b)

DISPLAY(c)

DISPLAY(d)

What is displayed as a result of executing the code segment?

  1. 21 40 30 50
  2. 10 20 30 40
  3. 21 40 30 40
  4. 21 30 40 50
Click for the answer! 1. because the first five statements assign values to the variables. The sixth statement assigns the value of x + b (which is 40) to b. The seventh statement assigns the value of x + 1 (which is 21) to a. The eighth statement assigns the value of c + d / 2 to d. According to the order of operations, division has higher precedence than addition. Since c is 30 and d / 2 is 20, d is assigned the value 50. The last four statements display the values of a, b, c, and d.