Skip to content


Data Types & Variables

****THIS PAGE IS A WORK IN PROGRESS****

integer (int): You can represent values ranging from -2,147,483,648 to 2,147,483,647 with an integer.

examples:

I ate 24874 burgers last night.

John's lives 9823728 meters away from Susan, that's a lot of meters.

now in programming terms:

int burgers_eaten_last_night:24874
int john_distance_to_susan:9823728

float: stands for floating numbers, which are not real numbers like integers.

examples:

I am 26.678 years old
The campus is 34.4 kilometers away from where I live

in programming terms:

float age=26.678
float distance_to_campus=34.4

boolean (boolean): This data type can represent if a value is either true or false.

examples:

it is afternoon already
I can not swim

in programming terms:

boolean afternoon=true
boolean can_swim=false

Character: This is a letter, a number, any character really.

examples:
char initial='a'

Strings: Strings are basically an array of characters - meaning they are words or bodies of text.

examples:

Hi, I am John and I like cakes
OMGWTFBBQ!!1!!!one!eleven!!1!

in programming terms:

string greeting="Hi, I am John and I like cakes"
string internet_grammar_example="OMGWTFBBQ!!1!!!one!eleven!!1!"

As you can see, while declaring a variable we first specify it's data type, then the name we will use to reference it, then the value (optional). What we are doing on a lower level is that, we are telling the computer to reserve some real estate on the RAM so we can use it later.
So;

data type + variable name=declared variable

You do not need to assign a value to a variable right away, you can leave it blank and just declare the variable to fill in later which would look like:

int age;
char gender;
string name;
float height;

Variables have scopes, which means we can access some variables from only some parts of the program and some variables from all parts of the program. The ones we can access from some parts are called local variables, we will be dealing a lot with those.

Using the right data type is important for memory management reasons. If you are going to use a variable for representing a person's age, you do not need an integer since a person can not have a negative value as their age. For values that will only be positive, you can use uint, which stands for unsigned integer and rages from 0 to 4,294,967,295. That's still too much memory reserved for someone's age. A Byte would be better for use in this case which represents values from 0 to 255.

We will mostly be dealing with integers, floats, strings and booleans in these materials and won't worry too much about memory management since our programs will be smaller on the scale but it is good practice to think ahead and use the right datatype for the right job.


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.