Variables
int number = 1;
Variables are named objects of a specified data type that we can assign a value to.
Usage
Declaring Variables
Declaring (or defining) a variable means giving it a name. You can declare a variable this way:
Initializing Variables
Initializing a variable means giving it an initial value.
We can do just that with the =
sign:
Data Types
Each variable has a Data Type (also known as a primitive), such as...
Name
Description
Example Value
var
Default data type. The compiler will assume the most likely data type.
5
20.3f
true
int
Integer. A whole number between -2147483648 and 2147483647.
24
float
Floating-point. A decimal between 1.175494351 E - 38 and 3.402823466 E + 38.
24.01f
bool
Boolean. A true or false value. Can also be represented by 1 or 0.
true
1
You can initialize a variable with a specific data type if you want:
In most cases, the compiler will assume a number is an int
.
You can put an f
after any number to show that it's a float
, even without a decimal point:
Conclusion
Now you know how to save whole numbers, decimals, and true/false values and refer to them by name. This will be very useful for re-using values in mathematic expressions and procedures.
With that in mind, you're ready to learn about a major component of procedures and variables.
Last updated
Was this helpful?