Computer Programming: How Variables work 

Easy computer programming tutorial for beginners on working with variables.

What is a ‘Variable’ in computer programming?

Image a box.

Image for post
Image for post

Type of a variable

Although you can give your variable box any name you want, the value it can hold can only be of a certain type.

Type of value must equal the type of variable

Like a wooden “toy box” in the real world, a variable box can be one of several types — mainly the 8 primitive data types.

  1. char — A variable of data type char can hold a single character.
  2. double — A variable of data type double can hold decimal value integer.

Naming a variable

Of course you can give it any kind of name you want, however, it is good code practice to begin the name [of a variable] with only the first letter written in small letters.

For example: I can declare a variable of type int and give it a name: “evenNumber” or “oddNumber” or just “number”.

int evenNumber;

Finally, how to use a variable in an actual program.

A) You must declare the variable

Declaring a variable simply means to define it by giving it a name and a type.

You have just learnt this. Here is another example:

Assigning a value to a variable is done through ‘assignment statements’.

This is easy.

QUIZ TIME

Let’s see what you have learnt so far. Use the comments section to submit your answers!

  1. Declare a variable of type char and assign it the value of the 9th letter of the alphabet.
  2. Declare a variable and assign it with the highest score of 13.5 in a game of Tetris.

If you have found this article useful and want to see more tutorial posts like this one, please hit that recommend button!

Written by

Join Our Tribe of 2100+ devs building something great @ www.hackernation.io

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store