SHOP

PLAY

CREATE

6

Operate with variables

Min. Age: 6+ Mother and son; age icon Duration: 20 min. Mother and son; age icon

Video Example:

Material required: Download material .pdf Code editor

program BEGIN block
BEGIN block
program END block
END block
Variable blocks
Variable blockd
Board game variable tiles
Variables
Random function call
'random' function call ('die roll')
Two dice: one dotted and another with numbers
Die of choice
Print variable function call
'print' function call
Number tiles for board game
Numbers
Board game arithmetic operators
Arithmetic operators
Assignment block
Assignment block
Code block
Code blocks
Whiteboard and marker
Whiteboard and marker

  Read       Write

1

How do I assign the result of an addition to a variable?

Assign the result of an addition to variable 'a' using an assignment block as follows:

1

"2 + 1 = 3; assigning the value '3' (result of the addition) to variable 'a'

Board Game Block program assigning the result of an addition to a variable
2

"Calling the 'print' function, Printing the value of variable 'a' "

Board Game Block program printing the result of an addition
3

Now try performing other additions and other operations.

2

How do I assign the result of another type of operation to a variable?

1

Subtraction: " 3 - 1 = 2; assigning the value '2' to variable 'a'

Board Game Block program assigning the result of an subtraction to a variable and printing it
2

Multiplication: "3 x 2 = 6; assigning the value '6' to variable 'a' "

Board Game Block program assigning the result of a multiplication to a variable and printing it

Use the symbol '*' to multiply two numbers.

3

Division: "6 / 2 = 3; assigning the value '3' to variable 'a' "

Board Game Block program assigning the result of a division to a variable and printing it

Use the symbol '/' to divide two numbers.

4

Remainder of a division: "5 / 3 = 1 with a remainder of 2; therefore 5 % 3 = 2; assigning the value '2' to variable 'a' "

Board Game Block program assigning the remainder of a division to a variable and printing it

Use the symbol '%' (modulo operator) to calculate the remainder of a division between two numbers.

5

Adding the return of the 'random' function to a number: "Calling 'random' function; the result of the call is '3'; 3 + 2 = 5; assigning the value '5' to variable 'a' "

Board Game Block program: Adding the return of the 'random' function to a number, assigning the result of the addition to a variable and printing it
3

How do I modify the value of a variable?

To modify the value of a variable, assign a value to variable 'a' using an assignment block, afterwards assign another value to variable 'a' using another assignment block.

Example:

1

Place the blocks as follows

Board Game Block program assigning the result of an addition to a variable
2

"Assigning value 2 to variable 'a' "

Board Game Block program to modify the the value of a variable
3

Continue reading the program in the direction of the arrows Direction arrows of an assignment block

4

"Assigning value 6 to variable 'a' ". You've just modified the value of variable 'a' !

Modifying the the value of a variable using a block programming board game
4

Can I use the value of variable 'a' in an operation?

Yes, you can:

1

- " 3 x 2 = 6; assigning the value '6' to variable 'a' "

- "Calling the 'print' function; printing the value of 'a' on the board."

Board Game Block program assigning the result of an multiplication to a variable and then substracting 1 and printing all steps
2

- "Given a = 6; then: a - 1 = 5; assigning the value '5' to variable 'a' "

- "Calling the 'print' function; printing the value of 'a' on the whiteboard."

Decrementing the the value of a variable and printing it using a block programming board game
5

How do I print the value of a variable in my terminal?

1

Write the programs in the editor and press RUN to execute them.

Note: We have chosen JavaScript as the programming language so that you don't have to install anything on your computer.

2

Use curly braces { } to delimit code blocks. These blocks can contain multiple instructions.

3

Code comments: Use ' // ' and ' /* */ ' to comment the code.

4

Assign value:Declare a variable 'a' and assign the value 3. Then print the value of 'a' using 'console.log'.

5

Addition: Print the result of an addition:

6

Other operations: Print the result of other operations: Modify the value of 'a' in each operation.

7

Assign a random value to a variable: Declare a variable 'a' and assign a random value between 1 and 6 (by calling the 'random' function).
Then print the value of 'a' using 'console.log'.
Next, do the same but subtract a number from the result of the 'random' function call.

8

Using variables in operations: Assign a value to variable 'a' and increment it by 1, then multiply it by 3:

9

Using variables in operations (2): Assign a value to a variable 'a'. Then assign a value to a variable 'b'. Finally sum the two values and print the result:

Puzzles:

Read 1

7+ years

Read the following code and answer the question: What will be the value of variable 'a' once the two assignments have been executed?

Give the value of a variable after two assignments
Show answer

Answer:

4

Write 2

11+ years

Using the following blocks, write a code where the variable 'a' starts at 3 and ends at 12.

Blocks for programming three assignments of one variable
Show one possible solution

Possible solution:

Block programming: arithmetic operations on a variable using three assignments

Read  3

12+ years

What is the value of variable 'a' once all assignments have been executed?

Show answer

Answer:

2

Write 4

12+ years

Complete the ' // ... ' in the code with assignments so that variable 'a' ends up being equal to 7.

Show one possible solution

Possible solution:


Debug5

8+ years

"Thank goodness you're here, I have a problem! Can you fix the code so that the program prints the number '10' instead of '5' after going through the three assignments? "

Thinking developer because of a bug
Block programming: program printing the value 5 after three arithmetic operations
Show one possible solution

Possible solution:

Block programming: Program printing the value 10 after three arithmetic operations

Debug6

12+ years

"Something went wrong! I expected that after executing the following code, the value of 'a' would be 1, but it's not. Can you fix the code by changing only one arithmetic operator, so that 'a' equals 1 after going through the five assignments?"

Thinking developer because of a bug
Show one possible solution

Possible solution: