Migrate docs from the other repo
This commit is contained in:
40
docs/reference/variables/assign.md
Normal file
40
docs/reference/variables/assign.md
Normal file
@ -0,0 +1,40 @@
|
||||
# Assignment Operator
|
||||
|
||||
Set the value for local and global variables #docs #assignment #language #var #data
|
||||
|
||||
### @parent blocks/operators
|
||||
|
||||
Set or change the value of a variable
|
||||
|
||||
````blocks
|
||||
let item = 0
|
||||
````
|
||||
|
||||
Use the assignment operator to set or change the value of a [variable](/microbit/reference/variables/var).
|
||||
|
||||
### Declare a variable
|
||||
|
||||
Declare a new *local* variable using the [variable](/microbit/reference/variables/var) statement and the assignment operator. Like this:
|
||||
|
||||
````blocks
|
||||
let num1 = 42;
|
||||
let name = "Joe";
|
||||
````
|
||||
|
||||
The variable's name is on the left of the assignment operator and the variable's value is on the right:
|
||||
|
||||
````blocks
|
||||
let num1 = 42
|
||||
````
|
||||
### Notes
|
||||
|
||||
* You can use the assignment operator with variables of each of the supported [types](/microbit/blocks/types).
|
||||
|
||||
### Lessons
|
||||
|
||||
[rotation animation](/microbit/lessons/rotation-animation)
|
||||
|
||||
### See also
|
||||
|
||||
[variable](/microbit/reference/variables/var), [types](/microbit/blocks/types)
|
||||
|
40
docs/reference/variables/change-var.md
Normal file
40
docs/reference/variables/change-var.md
Normal file
@ -0,0 +1,40 @@
|
||||
# Change Value
|
||||
|
||||
Set the value for local and global variables #docs #assignment #language #var #data
|
||||
|
||||
### @parent blocks/change-value
|
||||
|
||||
Change the value of a variable
|
||||
|
||||
```blocks
|
||||
let x = 0
|
||||
x += 1
|
||||
```
|
||||
|
||||
### Declare a variable
|
||||
|
||||
Use the assignment operator to set the value of a [variable](/microbit/reference/variables/var). Change the value of a variable from 0 to 1 using the change item block. Like this:
|
||||
|
||||
```blocks
|
||||
let x = 0
|
||||
x += 1
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
Use the assignment operator to set the value of a [variable](/microbit/reference/variables/var). Change the value of a variable from 0 to 1 using the change item block. Then display the new value of the variable on the LED screen. Like this:
|
||||
|
||||
```blocks
|
||||
let x = 0;
|
||||
x += 1;
|
||||
basic.showNumber(x);
|
||||
```
|
||||
|
||||
### Notes
|
||||
|
||||
* You can use the assignment operator with variables of each of the supported [types](/microbit/blocks/types).
|
||||
|
||||
### See also
|
||||
|
||||
[variable](/microbit/reference/variables/var), [types](/microbit/blocks/types)
|
||||
|
92
docs/reference/variables/var.md
Normal file
92
docs/reference/variables/var.md
Normal file
@ -0,0 +1,92 @@
|
||||
# Local Variables
|
||||
|
||||
How to define and use local variables. #docs #var #language #variables
|
||||
|
||||
### @parent language
|
||||
|
||||
A variable is a place where you can store and retrieve data. Variables have a name, a [type](/microbit/blocks/types), and value:
|
||||
|
||||
* *name* is how you'll refer to the variable
|
||||
* *type* refers to the kind of data a variable can store
|
||||
* *value* refers to what's stored in the variable
|
||||
|
||||
### Var statement
|
||||
|
||||
Use the Block Editor variable statement to create a local variable and the [assignment operator](/microbit/reference/variables/assign) to store something in the variable.
|
||||
|
||||
For example, this code stores the number `2` in the `num1` variable:
|
||||
|
||||

|
||||
|
||||
Here's how to define a variable in the Block Editor:
|
||||
|
||||
1. Click `variables`.
|
||||
|
||||
2. Change the default variable name if you like.
|
||||
|
||||
3. Drag a block type on the right-side of the [assignment operator](/microbit/reference/variables/assign) and click the down arrow to change the variable name.
|
||||
|
||||
The resulting code should look something like this:
|
||||
|
||||
// string variable
|
||||
|
||||

|
||||
|
||||
// number variable
|
||||
|
||||

|
||||
|
||||
// boolean variable
|
||||
|
||||

|
||||
|
||||
// image variable
|
||||
|
||||

|
||||
|
||||
See [Image](/microbit/blocks/image) for info on creating and using image variables.
|
||||
|
||||
The resulting code should look something like this:
|
||||
|
||||

|
||||
|
||||
A variable is created for the number returned by the [brightness](/microbit/reference/led/brightness) function.
|
||||
|
||||
### Using variables
|
||||
|
||||
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:
|
||||
|
||||

|
||||
|
||||
To change the contents of a variable use the assignment operator. The following code sets `counter` to 1 and then increments `counter` by 10:
|
||||
|
||||

|
||||
|
||||
### Why use variables?
|
||||
|
||||
Variables help simplify your code. For example, instead of turning on LEDs one by one like this:
|
||||
|
||||

|
||||
|
||||
You can use a variable (`i`) and a [for loop](/microbit/reference/loops/for) to plot the same series of points (`i` is incremented by 1, each time the loop repeats):
|
||||
|
||||

|
||||
|
||||
### Local variables
|
||||
|
||||
Local variables exist only within the function or block of code where they're defined. For example:
|
||||
|
||||

|
||||
|
||||
#### Notes
|
||||
|
||||
* 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".
|
||||
|
||||
### Lessons
|
||||
|
||||
[glowing pendulum](/microbit/lessons/glowing-pendulum), [love meter](/microbit/lessons/love-meter), [temperature](/microbit/lessons/temperature), [zoomer](/microbit/lessons/zoomer)
|
||||
|
||||
### See also
|
||||
|
||||
[types](/microbit/blocks/types), [assignment operator](/microbit/reference/variables/assign)
|
||||
|
Reference in New Issue
Block a user