Problems

Imagine that you have just been given one hundred arithmetic problems to do and you need to work out all of the answers before you can have any leisure time. You know exactly how to do them but completing so many is going to be very time consuming. By the time you are done there will likely be not much time remaining for leisure.

Imagine that your arithmetic worksheet contained some problems like this:

  • 5+4=
  • 7+2=
  • 5×4=
  • 20÷5=

If only there was a way for you to give clear instructions to a machine so that it can work out all the answers instead of you. This would be much better because you could set it up, start it and then leave it to do the work. You would be secure in the knowledge that it won’t get bored and start making mistakes. It will be able to work out the answers much more quickly than you would be able to. Most importantly, you will be free to use all the remaining time however you see fit!

Problems like those in your arithmetic worksheet are solvable by computers. This is because, for those problems, you can give a computer a set of unambiguous instructions that it needs to follow. When it does, it will always get the correct results.

Programs

Such instructions are called computer programs (sometimes also called software or applications (apps)).

Computer programs are made of:

  • Variables
  • Expressions
  • Statements
  • Decisions
  • Loops

Variables and Expressions

The first two problems in your worksheet are very similar, the computer needs to do the same + calculation (but the values that it needs to use are different (5 and 4 for the first, 7 and 2 for the second)).

Where problems are similar but have different values, we can use something called variables in place of the actual values. In computer programming, a variable can be used to store a single value. We can give variables names to identify them and the value we want to store is assigned to that variable.

A variable can be visualised as a box in the computer’s memory. The box is labelled with a name to identify it and it contains a value.

An image with two boxes, the first box is labelled "num1" on the front. Inside the box a piece of paper with a "7" is visible., the second box is similar butis labelled "NUM2" and contains a piece of paper with a "2".
A representation of two variables containing numeric values.

We can now write program statements that we can use to solve any + problems:

ans = num1 + num2
print(ans)

In this program are three variables, ans, num1 and num2.

num1 and num2 are used to store whatever the values in the problem are, ans (short for answer) will be used to store the result.

In the first example problem, “5+4=” the 5 will be assigned to the variable identified as num1 and the 4 to the variable identified as “num2”.

The second program statement print(ans) simply displays the value that is stored in this variable on the screen (the result of the calculation).

To solve the second problem we use the same program statements but this time the values stored in num1 and num2 are different, as is shown in the picture above.

Expressions and Decisions

The program we have made so far only works on + type problems. There are other problems as well such as × and ÷ and we need our program to work on these types of problem as well.

We need our program to make a decision about what type of calculation it needs to carry out based on the type of problem.

if op is + then ans = num1 + num2
else if op is × then ans = num1 * num2

Our program is now starting to be more capable. We have created a new variable called op (short for operator). This new variable is used to store the type of problem (+ or × or ÷)

We have also introduced some new program words, these words are “if” and “else if“. These words allow the computer to make a decision.

The computer makes this decision by evaluating (checking the result) of an expression.

Take the problem 5×4=

  • The computer will start with the instruction if op is + then (because this is the first instruction in the program).
    • op is +” is an expression, either it is + or it is not + (in other words it evaluates as True or False)
    • Here, op is not + so the program will not do the ans = num1 + num2 statement.
  • The computer will just skip to the next part of the decision instead: else if op is × then
    • Here, op is × so the computer will carry out ans = num1 * num2

We can complete this part of the program so that is can also do a ÷ problem like this:

if op is + then ans = num1 + num2
else if op is × then ans = num1 * num2
else if op is ÷ then ans = num1 / num2
print(ans)

Any of the three types of problem we might have in the worksheet can now be solved. The solution will be stored in the variable named ans. Finally, whatever that value is, this will be displayed on the screen.

Loops

Our program is almost complete! There is one final hurdle to be overcome. At the moment, our program can only solve a single problem. Our worksheet contains 100 problems! We need to tell our program to keep repeating the process for every problem in the sheet. This process is referred to as looping or iteration.

Our finished program might look like this below.

for each problem:
  if op is + then ans = num1 + num2
  else if op is × then ans = num1 * num2
  else if op is ÷ then ans = num1 / num2
  print(ans)
next

This means all the statements between

for each problem

… and …

next

will be repeated. The program will calculate and display the result for the first, then the next and next and so on until all the problems have been solved.

The programs we have written here is an example of high level programming. There are many high level programming languages with names like Python, Java, Rust, Visual Basic or C# (C Sharp). Each language has slightly different conventions but use the same methods. If you learn to program in a particular language the same ideas can be applied to a different language.

This example here is not a real programming language, it has been made up to help explain the basic concept of computer programming. We could, if we wanted to, create instructions for a computer to follow that would allow it to run a program like the one in this example. If we did this then we would have created a new programming language of our own.

High Level and Low Level Languages

Computers can only work with one type of data, zeroes and ones (binary). This means that for a computer to execute a program like the one in this example, first it needs to translate the program into its equivalent binary code. This binary code is known as machine code. Machine code is an example of a low level language.

High level languages have been made with human beings in mind, we can easily create programs in high level languages.

Low level languages have been made specifically for computers. Human beings are able to write programs in low level languages but it is much more difficult for them to do this so we usually use high level languages and a special program called a translator to convert our high level programs into low level programs.

Once a high level program has been translated into a low level language (like machine code) then the computer is able to follow the instructions in that program.

By admin