You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/blocks/variables/assign.md
+26-3Lines changed: 26 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -1,11 +1,34 @@
1
1
# Assignment Operator
2
2
3
-
Use an equals sign to make a [variable](/blocks/variables/var) store the [number](/types/number)
4
-
or [string](/types/string) you say.
3
+
Use an equals sign to make a [variable](/blocks/variables/var) store a [number](/types/number), [string](/types/string), or other [type](/types) of value.
5
4
6
-
When you use the equals sign to store something in a variable, the equals sign is called
5
+
When you use the equals sign (**=**) to store something in a variable, the equals sign is called
7
6
an *assignment operator*, and what you store is called a *value*.
8
7
8
+
When you work in JavaScript or Python the equals sign is used:
9
+
10
+
```typescript-ignore
11
+
item = 3
12
+
```
13
+
14
+
## The 'set' block
15
+
16
+
In blocks, a variable assignment happens with the ``||variables:set||`` block when you set a value to a [variable](/blocks/variables/var).
17
+
18
+
```block
19
+
let item = 3
20
+
```
21
+
22
+
### ~ hint
23
+
24
+
#### Setting a variable using the Toolbox
25
+
26
+
In the ``||variables:Variables||`` category of the **Toolbox** you can create or choose a variable to assign:
27
+
28
+

29
+
30
+
### ~
31
+
9
32
## Storing numbers in variables
10
33
11
34
This program makes the variable `item` equal `5` and then shows it on the [LED screen](/device/screen).
Copy file name to clipboardExpand all lines: docs/blocks/variables/var.md
+75-31Lines changed: 75 additions & 31 deletions
Original file line number
Diff line number
Diff line change
@@ -1,55 +1,77 @@
1
-
# Local Variables
1
+
# Declaring variables
2
2
3
-
How to define and use local variables.
3
+
How to declare and use variables.
4
4
5
5
## @parent language
6
6
7
-
A variable is a place where you can store and retrieve data. Variables have a name, a [type](/types), and value:
7
+
A variable is a place where you can store and retrieve data. Variables have a name, a [type](/types), and a value:
8
8
9
9
**name* is how you'll refer to the variable
10
-
**type*refers to the kind of data a variable can store
11
-
**value*refers to what's stored in the variable
10
+
**type*is the kind of data a variable can store
11
+
**value*is what's stored in the variable
12
12
13
-
## Var statement
13
+
## The variable (var) statement
14
14
15
-
Use the Block Editor variable statement to create a variable
16
-
and the [assignment operator](/blocks/variables/assign)
17
-
to store something in the variable.
15
+
A variable is created using a `variable` statement. The variable will "declare" itself in this statement. In MakeCode JavaScript a variable is declared along with its first [assignment](/blocks/variables/assign):
18
16
19
-
For example, this code stores the number `2` in the `x` variable:
17
+
```typescript
18
+
let x =2
19
+
```
20
+
21
+
With Python a variable is declared when it's first used. In this case the variable statement is just the assignment of the variable:
22
+
23
+
```python
24
+
x =2
25
+
```
26
+
27
+
If a variable is declared in blocks you will see the ``||variables:set||`` block with the first use of the variable. This code stores the number `2` in the `x` variable:
20
28
21
29
```blocks
22
-
let x = 2;
30
+
let x = 2
23
31
```
24
-
Here's how to define a variable in the Block Editor:
25
32
26
-
1. Click `variables`.
33
+
The new variable is created inside the ``||variables:Variables||`` category of the **Toolbox**.
34
+
35
+
### ~ hint
27
36
28
-
2. Change the default variable name if you like.
37
+
#### Creating a variable from the Toolbox
29
38
30
-
3. Drag a block type on the right-side of the [assignment operator](/blocks/variables/assign) and click the down arrow to change the variable name.
39
+
In the ``||variables:Variables||`` category of the **Toolbox** you can create new variable:
40
+
41
+

42
+
43
+
Here's how to create a variable using the Toolbox:
44
+
45
+
1. Click ``||variables:Variables||`` in the Toolbox.
46
+
2. Click on **Make a Variable...**.
47
+
3. Choose a name for your variable, type it in, and click **Ok**.
48
+
4. Drag the new variable, ``||variables:set||`` or ``||variables:change||`` block into your code.
49
+
50
+
### ~
51
+
52
+
### Quick example
31
53
32
54
A variable is created for the number returned by the [brightness](/reference/led/brightness) function.
33
55
34
56
```blocks
35
-
let b = led.brightness();
57
+
let b = led.brightness()
36
58
```
37
59
38
60
## Using variables
39
61
40
62
Once you've defined a variable, just use the variable's name whenever you need what's stored in the variable. For example, the following code shows the value stored in `counter` on the LED screen:
41
63
42
64
```blocks
43
-
let counter = 1;
44
-
basic.showNumber(counter);
65
+
let counter = 1
66
+
basic.showNumber(counter)
45
67
```
46
68
47
69
To change the contents of a variable use the assignment operator. The following code sets `counter` to 1 and then increments `counter` by 10:
48
70
49
71
```blocks
50
-
let counter = 1;
51
-
counter = counter + 10;
52
-
basic.showNumber(counter);
72
+
let counter = 1
73
+
counter = counter + 10
74
+
basic.showNumber(counter)
53
75
```
54
76
55
77
## Why use variables?
@@ -58,11 +80,11 @@ If you want to remember and modify data, you'll need a variable.
58
80
A counter is a great example:
59
81
60
82
```blocks
61
-
let counter = 0;
62
-
input.onButtonPressed(Button.A, () => {
63
-
counter = counter + 1;
64
-
basic.showNumber(counter);
65
-
});
83
+
let counter = 0
84
+
input.onButtonPressed(Button.A, function () {
85
+
counter = counter + 1
86
+
basic.showNumber(counter)
87
+
})
66
88
```
67
89
68
90
## Local variables
@@ -72,16 +94,38 @@ Local variables exist only within the function or block of code where they're de
72
94
```blocks
73
95
// x does NOT exist here.
74
96
if (led.brightness() > 128) {
75
-
// x exists here
76
-
let x = 0;
97
+
// x exists here
98
+
let x = 0
77
99
}
78
100
```
79
101
80
-
### Notes
102
+
## Notes about variables
103
+
104
+
### Variable names
81
105
82
-
* You can use the default variable names if you'd like, however, it's best to use descriptive variable names. To change a variable name in the editor, select the down arrow next to the variable and then click "new variable".
106
+
Some blocks come from the Toolbox with default variable names, such as `list` from ``||arrays:Arrays||``.
107
+
You can use the default variable names if you like, however, it's best to use descriptive variable names. To change a variable name in the editor, select the down arrow next to the variable and then click **"Rename variable..."** to change it.
108
+
109
+
### Hidden declaration block
110
+
111
+
If a variable is used more than once, and its declaration block sets the variable to `0`, that first declaration block is hidden. For example:
112
+
113
+
```blocks
114
+
let x = 0
115
+
if (x == 0) {
116
+
x = 9
117
+
}
118
+
```
119
+
120
+
You don't see any first block setting the variable `x` to `0` but that happens in its hidden declaration statement. You'll see the declaration statement, `let x = 0`, if you switch from Blocks to JavaScript in the Editor:
In the ``||variables:Variables||`` category of the **Toolbox** you can create new variable:
6
6
7
-
For the @boardname@, ASCII character codes 32 to 126 are supported; letters, digits, punctuation marks, and a few symbols. All other character codes appear as a ? on the [LED screen](/device/screen).
7
+

8
8
9
-
## ~
9
+
Here's how to create a string variable using the Toolbox:
10
+
11
+
1. Click ``||variables:Variables||`` in the Toolbox.
12
+
2. Click on **Make a Variable...**.
13
+
3. Choose a name for your variable, type it in, and click **Ok**.
14
+
4. Drag the new ``||variables:set||`` block into your code.
15
+
5. Click on the ``||text:Text||`` drawer in the Toolbox and find the ``||text:" "||`` block.
16
+
6. Drag the ``||text:" "||`` block into the value slot in of your variable ``||variables:set||`` block.
17
+
18
+
## Characters you use in strings #custom
19
+
20
+
### ~ hint
21
+
22
+
#### Character sets
23
+
24
+
The available characters to use for a language is called the _character set_. Each character in the set has a number code to match it with.
25
+
To display characters on the [LED screen](/device/screen), the @boardname@, uses the "ASCII" character codes of `32` to `126`; letters, digits, punctuation marks, and a few symbols. All other character codes appear as a `?` on the LED screen.
0 commit comments