Basic Programming Principles

What is a variable?

A "variable" in computer terminology is defined as a container used to store information. It is given a unique name (identifier). A name given to a variable is a method of identifying a storage location in the computer. The term "variable" is used as the content stored in the variable may change or vary as the programme executes.

Variable names are case sensitive and must begin with a letter or the underscore character, e.g. streetAddress. When creating a variable it is assigned a value. The variable statement i.e. var, may or may not be used.

e.g. var streetAddress = "street" or streetAddress = "street"

In this example the name streetAddress has been given to the variable and it has been assigned the value of "street". So wherever the programme comes across the variable named "streetAddress" it will give it the value associated with the name called "street".

What are control structures?

Control structures are commands that control decisions and loops within programming language. The two main types of control structures are known as "decision" and "looping" structures.

Decision control structures perform actions based on test results of specified conditions. There are three basic decision control commands which are If ... Then ... Else ...

"If" and "Then" statements execute one or more statements based on a certain condition being satisfied.If a certain condition is met, then perform a specified statement. The condition can be a comparison or any expression that evaluates to a specific value. The condition is evaluated to be either True or False. If the condition is True then statements following the Then keyword are performed. If the condition is False then statements following the Else keyword are performed.

A simple example of this could be:

if purchase_total >= $250

then delivery_charge = $0

else delivery_charge = $25

endif

This statement is saying if the purchase total is greater than or equal to $250.00 then the delivery charge is to be $0.00, if the statement is false, the delivery charge is to be $25.00.

Looping control structures allows one or multiple lines of code to be executed in a repetitive manner. The most basic methods for looping are While ... Loop ... statements and For ... Next ...statements.

While ... Loop ... statements are performed while a condition is True. There are several variations of this kind of looping structure but each variation evaluates a condition to decide whether or not to continue to execute the loop. If the condition specified by the statement is False then it exits the loop and goes to the next program step.

For ... Next ... statements work well when you know the exact number of times you want to perform a loop. The For loop uses a counter variable that increases or decreases in value every time the loop is repeated.

Control structures can also be 'nested' i.e. you can put command structures inside other command structures, for example, you can put an If...Then statement inside a For...Next loop.

Now let's take a look at object-oriented programming (OOP).