# Programming Concepts

## Variables and Constants

You have probably already come across the idea of __variables __in the programming that you have done so far on the course. Variables allow you to refer to data stored in memory when writing code, and by their very name it is obvious that variables can be changed as the program is processed by the computer. For example at the beginning of a game we might set the variable top_score to 134 but, if somebody scores 137, we will have to change top_score to 137.

A variable is a location in memory which stores a value that can be changed.

Sometimes it is useful to store a value that doesn’t change during the processing of the program. For example, if we want to keep 4 names on the high score list for a game, we would create a constant_ _NO_OF_HIGH_SCORES. A constant is like a variable because it holds a value but it is different because it never changes. Notice that constants usually have their names written in capital letters.

A constant is a location in memory which stores a value that does NOT change.

Hint: Never refer to a constant being a variable that doesn’t change - this will lose you marks in your exam.

There several good reasons for using a constant:

1. If later on we decide that we want a different number of high scores we only have to change the value of the constant once in the source code. If we didn’t have the constant we’d have to hunt through the program to find all the places where we’d written 4 and change them to the new number.
2. It makes sure that our program is consistent as we only have to enter the value once.
3. If lots of people are working on the same program it stops mistakes happening because some people think the number is one thing and some think it is another.
4. It also makes your code more readable. For example if you are writing a program to calculate with circles it is much easier to have a constant called PI than to have to keep typing 3.1415…

In some languages, we have to declare a variable or constant before assigning it a value. This is when we tell the program the data type and the identifier__ (name). We can then __assign it a value using the equals sign.

When we name variables and constants, it is really important that the names are descriptive and tell you what the variable or constant stores

## Main Constructs

Within programming, there are three main constructs:

1. Sequence - the idea of one step being done after another in a particular order
2. Iteration - repeating sections of code in a loop (FOR, WHILE, REPEAT)
3. Selection - changing the direction of a program based on a specific criteria (IF, SELECT CASE)

You will be expected to identify sections of algorithms that contain each of these things.

## Iteration

Indefinite Loop

If you don’t know beforehand how many times the loop will repeat, you use an indefinite loop. This is also called a condition-controlled loop because you decide whether to stop looping depending on how a condition is set.

It is very important in an indefinite loop for the condition to be changed in the loop. If it isn’t the condition will never be changed so the loop will never exit.

There are two types of indefinite loop:

1. Pre-controlled loops – the condition is checked before the loop starts, and will keep going whilst the condition is TRUE.
2. Post-controlled loops – the condition is checked once the loop has finished, and will stop as soon as the condition is TRUE.

Here are two examples of loops:

Pre-controlled loop

WHILE count < 5
count ← count + 1
ENDWHILE

Post-controlled loop

REPEAT
count ← count + 1
UNTIL count < 5

These two loops look similar (the condition is the same, and the count assignment is the same), but they will in fact do two completely opposite things. The first loop will keep going until count is 5 or more, whilst the second will only run if the count is 5 of more (and will only run once, or infinitely many times).

True or False?

Definite Loop

If you do know how many times the loop will repeat, you use a definite loop. This is also called a _counter-controlled__ _loop because the loop will repeat until the counter reaches a certain value.

Here is an example of a definite loop:

FOR count ← 1 TO 5
OUTPUT count
ENDFOR

This loop will run 5 times. Each time through the loop, count will increase by 1 until it reaches 5.

Nested Loops

We can also put loops inside one another. The loop in the middle will then repeat every time for the outside loop.

Here is an example of a nested definite loop:

FOR multiple ← 1 TO 10
OUTPUT multiple + “ times table”
FOR count ← 1 TO 12
OUTPUT count * multiple
ENDFOR
ENDFOR
How many times will the first OUTPUT appear?
10
How many times will the second OUTPUT appear?
120
A post-controlled loop will always run at least once.
True

## Selection

Selection refers to the ability to change the direction of a program based on a particular condition. One example of this is an IF statement. Within an IF statement, the only part that is compulsory is the initial IF statement, the ELSE IF and__ ELSE__ clauses are optional.

Here is an example of an IF statement using pseudocode:

1 IF count < 5 THEN
2 OUTPUT “Fail”
3 ELSE IF count < 10 THEN
4 OUTPUT “Pass”
5 ELSE
6 OUTPUT “Merit”
7 ENDIF

On the first line, the value of count is checked, and if it is less than 5, it will do what is immediately after it - in this case OUTPUT “Fail”. If it isn’t less than 5, then it will move onto line 3 and check the condition there. In this case, it will check whether the value is less than 10, and if so, it will carry out line 4. If it’s not less than 10, then it will move on to the next ELSE clause. In this case, there is nothing after the ELSE, so there is no condition to be checked, which means that the program will run line 6 regardless of the value of count.

Nested IF Statements

We can also use multiple IF statements within each other. This is called nesting.

1 IF count < 10 THEN

2 IF count < 5 THEN

3 OUTPUT “Fail”

4 ELSE

5 OUTPUT “Pass”

6 ENDIF

7 ELSE

8 OUTPUT “Merit”

9 ENDIF

In this case we have to be very careful about exactly which conditions are being checked. Indentation (how far it is from the left) will help to make this more readable.

## Relational Operators

Relational operators refer to being able to compare two values and return either TRUE or FALSE. There are 6 that you need to be aware of in both pseudocode and programming.

< Less than (3 < 5)

> Greater than (5 > 3)

= Equals (1 = 1)

≠ or !=__ __Not equals (1 ≠ 0)

or <= ____Less than or equal to__ __(3 ≤ 5 and 5 ≤ 5)

≥ __or >= __Greater than or equal to__ (5 __≥ 3 and 5 5)

## Operators

Arithmetic operators refer to mathematical calculations within a program. There are 6 that you need to be aware of in both pseudocode and programming.

• Subtraction

* Multiplication

/ Division - gives a real number (decimal) as the result

MOD ModulusFunction - finds the remainder when the first number is divided by the second

DIV Integer Division - finds the whole part when the first number is divided by the second

Most of these should be familiar to you. The ones that are a little trickier are the division calculations.

For example:

7 divided by 4 = 1 remainder 3

So…

7/4 = 1.75

7 MOD 4 = 3

7 DIV 4 = 1

## Boolean Operators

Boolean operators refer to being able to compare one or two boolean values and return either TRUE or FALSE. There are 3 that you need to be aware of in both pseudocode and programming. These are covered in a lot more detail in the Boolean Logic section.

NOT This reverses the input (NOT TRUE = FALSE)

AND This checks if both inputs are TRUE and returns TRUE __if so, __FALSE otherwise

OR This checks if either input is TRUE and returns TRUE if so, __FALSE __otherwise

How many times will the second OUTPUT appear?
120
How many times will the first OUTPUT appear?
10
A post-controlled loop will always run at least once.