Variables
Think of a variable as a box that holds a value. You can put a value in the box, take it out, and put a new value in. You can also change the value in the box as many times as you want.
This information can be anything you want to store, such as a number, a string of text, or a list of values. Variables make programs more powerful because they allow you to change and use stored data throughout your blocks.
Basic types
Each variable has a type that determines the kind of information it can store and how it can be used. Understanding variable types is important for knowing which type of variable to use in different situations.
Integer
The integer (int
) type is used to store whole numbers, both positive and negative.
Floating-point
The floating-point (float
) type is used to store decimal numbers or numbers with fractional parts.
String
The string (String
) type is an object that represents a sequence of characters. Strings are used to store and manipulate text data.
Boolean
The boolean (bool
) type is used to store logical values, which can be either true
or false
.
Set variable
The set variable block is used to update the value of a variable. The block has three inputs:
- type: The type of the variable.
- variable: The variable to update.
- value: The new value to assign to the variable.
Clicking on the type field will open a dropdown menu with the available types. To see an expansive list of all available types, select the Show all types option.
The variable field will display a dropdown menu with all the compatible variables.
To set a new value to a variable, connect the desired value block to the value input. Connected blocks must output a type that is compatible with the variable type.
Updating the type of a variable can lead to unexpected consequences. Not all types can be converted. For example, it doesn't make sense to convert text (String
) to a true/false value (bool
).
This can break your program and we will warn you if you try to do this.
Get variable
The get variable block is used to retrieve the value of a variable. Click on the variable field to open a dropdown menu with all the compatible variables.
The block will output the value stored in the selected variable.
Change variable by amount
The change variable by amount block is used to modify the value of a variable by adding, subtracting, multiplying, or dividing a specific amount.
The block has three inputs:
- variable: The variable to update. The variable must be a number type.
- operation: The operation to perform on the variable (
+
,-
,*
,/
). - value: The amount to add, subtract, multiply, or divide.
Advanced types
In addition to the basic types, there are more advanced types that offer slightly different functionality.
Integer
Integers can be divided into two types: signed and unsigned. Signed integers can store both positive and negative numbers, while unsigned integers can only store positive numbers.
Each type has a different size, which determines the range of values it can store. In the lists below, the types are ordered from smallest to largest. The size of each type is processor-dependent and can vary between platforms.
Unsigned integers are the same size as their signed counterparts, but they can store much larger positive numbers because they don't need to reserve space for negative values.
Signed integer
char
short
int
long
Unsigned integer
unsigned char
unsigned short
unsigned int
unsigned long
Floating-point
There are two sizes of floating-point numbers: single-precision and double-precision. Single-precision floating-point numbers are smaller and less precise than double-precision numbers.
You should use single-precision floating-point numbers when you need to save memory or when you don't need high precision. Double-precision floating-point numbers are more precise and are used when you need to store very large or very small numbers.
float
(single-precision)double
(double-precision)