benlund.co.uk

Business and newbie guide to programming


Variables – The corner stone for every code

VariablesVariables is one of the corner stone in programming. The variables a vital in every script you make. I will in this post describe what a variable is and how it can be useful. You can think of it as a part of a beginners guide to learning programming. If you have any form of experience as coder this post wont be much of a help to you. But if you are new to programming and has little or no experience then I suggest you tag along in this article.

What is a variable?

Variables can loosely be described as container of data that will label and store the data in the computer memory. You can at any time assign or extract data from a variable for you specific needs.

Naming variables

You can name the variable as you like. But it is a common practice to give it a name that will give you a hint of what data it will store. For example if you want to create a variable to store the age of a person a proper name for the variable could then be ”age”, ”personAge” or ”person_age”. If you should use ”_” or not to separate the two words in the variable name depends on what the common practice are for the programming language. A variable name can typically not start with a number.

Data types

A variable can store any form of data, but how that works can differ from language to language. I prefer to code in C and in that language you have to be very specific of how and what data you store in each variable.

When you set up a variable in C you must define the data type. Some of the the basic data types are integers, boolean, characters, float etc. I will not go in to deep in this article about the data types. But if you want a variable to only store integers you add ”int” before the variable name and character ”char” and so on. When you set a data type for a variable then the computer will allocated memory for the chosen type.

How to create and set up a variable

When setting up a variable you should always think of its purpose and what data it should contain. In some, but not all, languages you will need to define a data type when you set it up.

Se the examples below how you can set up a variables in C:

// In this example we first define the data type for the variable and then set the value
int person_age;
person_age = 23;

// You can define many variables at one by separating the variables with colon
int person_age, person_height, person_weight;
charperson_first_name , person_last_name, person_nick_name;

// If you have a initial data to store you can
// both define and add the value to a variable
int person_age = 23;

How to use a variable

There are endless ways to use a variable. The simplest way is to print out the value to the screen but you can also use a variable to calculate numbers, in a complicated loop or logic.

Here are some examples of how you can use variables in C:

// A simple example of how you print out a variable on the screen
printf(“Your age is: %d\n”, person_age);

// Simple calculation of how many days 23 years is and print out the result
int person_age _days = person_age * 365
printf(“Your are is %d days old\n”, person_age);

// How to use a varaible in a simple if…else statement
if(person_age > 30) {
printf(“You are old”);
} else {
printf(“You are young”);
}

Well there are the basic use for a variable and how to set it up. Of course there are alot more to say about how to use a variable but above are the basic examples.