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:

var variableName;

Initializing Variables

Initializing a variable means giving it an initial value. We can do just that with the = sign:

var variableName = 25;

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:

int variableName = 25;
float variableName2 = 25.01;

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:

// This will be assumed to be an int
var variableName = 25;
// This will be assumed to be a float
var variableName2 = 25f;

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.

pageScope

Last updated