Create Variables - Programming

What are Variables?

Variables are like containers that store something. Each container “type” possesses a set of properties including size, and what kind of content it can hold.

Two attributes that you should know:

Types

Each variable must have a “type.” This article will cover only the primitive types - char, short, int, long, long long, float, and double.

The primitive data type can hold only scalar values, no dimension to it. Choosing the right type is critical as the type affects how the variable is stored.

Declarations

Variables must be declared/created before you can use it.

e.g.

int height;
float amount;

Initialization means pre-populating the variable with an initial value:

e.g.

height = 5.0;
amount = 0.0;

Or, you can do both declaration and initialization together:

int height = 5.0;
float amount = 0.0;

Sample:

int length=5, width=10;
Brain.Screen.print("Perimeter of a %d by %d rectangle = %d",
length, width, (length + width)*2 );

Sample to show issues with something not initialized:

codesnippet1.png

Sample to show invalid declaration:

codesnippet2.png