Computer programming, coding and languages come in all shapes and form but like human languages they all have a common structure much like nouns, verbs and adverbs.  Once you learn programming skills and the fundamentals behind it you can generally pick up most langauges relatively easily. We could go on forever about the theory of programming languages but form here we’ll just jump right on in. For these tutorials I will write in psuedo code (PS) and also standard coding (SC). The standard coding is in no particular language but will resemble most modern langauges like Java, C++, C#, PHP or Python. Please bear in mind that the coding may differ from language to language.

In the next few sections we’ll look at the 4 basics of programming: Variables, Sequence, Conditional and Iteration. They may sound complicated but once you use them you’ll find the logic behind them incredibly simple to use. Today we’ll look at variables.

It’s likely you’ve heard of variables before if you’ve spent anytime around computers. They are essentially storage containers: they store numbers or letters within the computers memory. Exactly how this process works is unimportant for now, all we need to know is how to store something and retrieve it.

Variables should be written lowercase with the first letter capitalised if using more than more word. They also should describe what the variable is doing or what it relates to so coders can get a fair idea what it does. For example:

Right:

apples

applesAmount

amountOfApples

Wrong:

Apples

appleamount (not wrong but difficult to read)

ppp (again not wrong but meaningless)



Some variables are written all uppercase but we’ll look at those types another time.

To make a variable we usually have to declare and initialise it:

Set apples equal to 1 (pseudo code) or
apples = 1 (standard code)

This sets the variable apples to 1 and can be changed at any time. Notice the use of the ‘=’. In computing this can mean ‘set’ or ‘make equal to’ as well as ‘equals’. Yes it’s confusing and some languages make it more confusing by making ‘==’ mean ‘equals’. Bear this in mind when using the equals sign.

Set apples to 2 (pseudo code) or
apples = 2 (standard code)

You can print a variable to screen easily in most languages. This will print ’2′ on the screen.

Print apples or
echo apples
Output > 2

What can we store in a variable?

You can store anything you like in a variable like numbers, letters, words and references to other programmes in memory. The most common use is for numbers and words. When words are held in variables they are called ‘strings’ and they work in a similar way to numbers except they usually will require quotations to signify they’re strings.

Set names to “mark” or
names = “mark”

Printing the string usually works in exactly the same way as numbers.

Print names  or

echo names

Output > mark

So you can now do:

Print names and apples  or

echo names +”:” + apples

Output > mark:2

Notice how you can add your own strings within two variables, in this case the “:”

So far we’ve seen we can print variables but variables can also be manipulated with basic or complex maths. Our next chapter looks at manipulating variables and varible types.

Leave a Reply

You must be logged in to post a comment.