SciGo
Menu

Computer science > Programming and Algorithms

Algorithms and Interpreting Them

Algorithm - a precise method for solving a problem
(Command) Sequence - an ordered set of instruction
  • Pseudocode
  • Flowcharts
  • Python
<
>
Pseudocode (fake code) is used as a means of displaying an algorithm often to plan code. There is no set pseudocode, it is simply interpretable and translatable so it can be converted into a programming language. Pseudocode allows you to focus on the logic of a problem without worrying about the rules of a programming language.
​We are using our own Pseudocode on our pages (inspired by Edexcel Pseudocode)
Picture
Image from BBC Bitesize
Python is a high-level programming language (more specifically a scripting language) which can be used for a variety of tasks (making it a general purpose language).

When programmers learn a new language, it is customary to write a "Hello World" program. A "Hello World" program does practically nothing apart from outputting the words "Hello World" to the screen. It is a good way to learn the general syntax of the language and get a feel of what writing in the language might be like.

In Python, a "Hello World" program is incredibly simple to write, requiring only one line.
helloWorld.py

    
Great! If all is well, you should see the words "Hello World" outputted to the screen (this will change depending on how you are choosing to edit your python code). If this doesn't happen, you might get an error. This is most likely not a big deal and you should compare your code with the above to ensure that it is typed correctly. Make sure that print doesn't have a capital 'P' and there are two speech marks and brackets.

Variables

Variables are very important in any programming language, and Python is no exception. We can think of variables as a box. Our box will obviously store something and might have a label. This label will tell us what is inside the box and if we open our box, then we can do whatever we want with the contents. A variable is like this. When we declare a variable, we create a point in our computer's memory which will contain a value. We will also give our variable a name which our program will recognise the next time it comes up, and it will "open the box" (by visiting the variable's memory address) to see what is inside.

Python is a weakly typed language which means that we don't need to specify the data type (we will talk more about data types in a second).
helloWorld.py

    
Now we have created a variable called myVariable, containing the contents of the words "Hello World", which we can use in our program:
helloWorld.py

    
The above code is essentially doing what our first code snippet did, except now we have created a variable containing what we want to output, and the computer uses that to find out what to output.

Data Types

We have created a variable called myVariable which contains some words. That's great, but what if we want to store something else, such as a number? Here, we must use a different data type.
Data types are what you might think they are, types of data. They are as follows:
Name
Description
Examples
Int (integer)
A whole number that can be either positive or negative
96, 21, -2
Float / Real (Floating point)
A decimal number
1.5, -2.9, 3.1415
String
A collection of characters, represents words or numbers with no meaning (such as telephone numbers)
"John Doe", "Hello World", "Apple"
Boolean
A true or false value
True, False
Please note that the above table applies to Python. Other programming languages (such as C and C++) have various other data types but these data types follow the same principles as the above, it's just the binary representation of the data that is different.

Let's write a simple program to see all these data types in action
factFile.py

    
The above program creates a variable called name, containing a string, an age, containing an int, a height in feet and inches, containing a float and a likesPython variable which contains a boolean.

The above program is also quite boring however, so let's make our user enter in our data and output our factfile. This requires the use of a new function called the input function, which will take whatever our user enters in and puts it in a variable. Between the brackets we can put in a message that the user will see.
factFile.py

    
Note the use of the plus to append our string. Because we want our output format to be something along the lines of 'Name: <your name>', we need to add our name variable to the end of our 'Name: ' string and print that out.

When you run your program now, you will see that you will be prompted to enter in something. Type it in and press enter.

There are several issues with our program, though. Firstly, we can enter in anything we want because there are no restrictions on what we can enter. Secondly, the input function will give us a string, which means that all our variables are of the string data type and if we wanted to add 3 to our age for example, we would get an error because it is not an integer. Let's fix this by casting our variables to the appropriate data type.

Casting will convert our variable from one data type to another. However, we will get an error if our new data type cannot properly represent the old data. For example converting the string "100" to an int is fine, but converting the string "One hundred" to an int is not.
factFile.py

    
Let's see what our program is doing:

We take in a name which is already a string so needs no casting.
Then we take an age and convert it to an int.
Then we take a height and convert it to a float.
Then we take in whether or not the user likes python and convert to a boolean.

Now we output our name which is already a string so we can append it with no issues.
However, our age is an int and needs to be converted into a string in order to be appended, so we cast it to a string and do the same with the others.

Control Flow

Until now, our program has been running line by line (see the sequence programming construct below). However, sometimes we need bits of code to be run multiple times, or only under some conditions. This is called control flow.

First, we will look at if loops. If loops take in a boolean value and evaluate whether it is true or false, only running a designated piece of code if it is true. The syntax for this is below:

    

Programming Constructs

​Variables
​Variables are stores of data. Each variable stores one piece of data under a variable name and can be changed or used throughout a program. Variables can be declared and initialised:
Declaring - Setting the type of data the variable stores
Initialising - Setting the data a variable stores
  1. SET age TO [INT] 16
  • In the above line of pseudocode, the variable (age) is being declared as an integer and initialised as 16
​Variables can be local or global. A local variable can only be used within the function/procedure that it was created in while a global variable can be used throughout the code.

Iteration
Iteration is repetition: When you repeat an algorithm or sequence, you could write it out again an again but it is significantly more efficient to use an iteration statement. These are for, while and repeat...until loops
  1. SET age TO (INT) 16
  2. FOR i FROM 1 TO 10:
  3. age += 1
  4. END FOR
The above pseudocode will add 1 to the value of age 10 times to get a final value of 26. (the variable 'i' starts at 1 and increases each time until it gets to 10). This is much more efficient than writing the third line out 10 times. This is also a form of definite iteration as the number of loops is known beforehand.
  1. SET age TO (INT) 16
  2. WHILE age > 10:
  3. age -= 1
  4. END WHILE
The above will subtract 1 from the value of age each time it checks the vale of age and finds it to be more than 10. In this case, the process will happen 6 times and stop when age = 10. While loops are useful for when the number of repeats depend on a factor which may change. As the number of loops is unknown beforehand, this is indefinite iteration.
  1. SET age TO (INT) 16
  2. REPEAT:
  3. age += 1
  4. UNTIL age>25
This will add 1 to age until it is more than 25, so it would do it 10 times till age is 26. In a repeat until, the condition is checked at the end of each loop. This means line 3 will always happen at least once. (Python does not have this form of iteration.) As the number of loops is unknown beforehand, this is indefinite iteration​.

Selection
Selection chooses between going through a block of code or not. This is done with if statements:
  1. SET age TO (INT) 16
  2. IF age = 16:
  3. SEND "Good Luck" TO DISPLAY
  4. END IF
In the above pseudocode, as age is equal to 16, 'Good Luck' will be displayed. If age was not 16, this would not be displayed.
  1. SET age TO (INT) 15
  2. IF age = 16:
  3. SEND "Good Luck" TO DISPLAY
  4. ELSE IF age = 15:
  5. SEND "Good Luck Person" TO DISPLAY
  6. ELSE:
  7. SEND "Hello" TO DISPLAY
In the above pseudocode, as age is not equal to 16, we skip the first if. age is equal to 15 so the program displays "Good Luck Person". If age was 14, it would ignore the else if as well and go to the if, displaying "Hello"

Subroutines
Subroutines are algorithms or modules for performing specific tasks. They are either functions or procedures.
Parameters are values that are passed into a subroutine when it is called.

Procedure
  • A subprogram that contains a set of instructions it carries out when called without returning a value to the main program.
  1. PROCEDURE add (a, b):
  2. SET sum TO a + b
  3. END PROCEDURE
This procedure adds together number a and number b. An example of it being called is:
  1. add (3, 7)
Function
  • A subprogram that contains a set of instructions it carries out when called that returns a value to the main program
  1. FUNCTION adding (a, b):
  2. SET sum TO a + b
  3. RETURN sum
  4. END FUNCTION
This function adds together number a and number b. An example of it being called is:
  1. c = adding (3, 7)
In the above line, c equals 10

In both cases, the parameters were a and b (3 and 7) but not all functions and procedures need parameters.

Errors and Checks

Photo used under Creative Commons from goodrob13
  • Science
    • Biology >
      • Reproduction
      • Blood and Circulation
      • Enzymes
      • The Skeleton and Muscles
      • Cells
      • Food and Digestion
      • Genetics
      • Respiration and Gas Exchange
      • Plant Metabolism - Photosynthesis and Respiration
      • Kidneys and Urinary system
      • Evolution
    • Chemistry >
      • Particles
      • Rocks
      • Atomic Structure and the Periodic Table
      • Rates of Reaction
      • Crude oil
      • Metals
      • Analysis
    • Physics >
      • Forces and Motion
      • Light and Sound
      • Energy Resources
      • Waves
      • Magnetism
  • Add Maths
    • Polynomials
    • Binomials
    • Permutations and Combinations
    • Graphs
  • Latin
    • Grammar >
      • Verbs
      • Participles
      • Nouns
      • Adjectives
    • Vocabulary
  • History
    • Battle of Hastings
    • World War Two and International Relations
    • Key Words- Medieval Era
    • The Tudor Church
    • The Spanish Armada
    • World War I
    • World War Two and International Relations
    • Why had international peace collapsed by 1939?
    • Glossary
  • Geography
    • The UK and The Republic of Ireland
    • Geographical Skills >
      • Ordnance Survey
    • UK Economy
    • Development
    • Globalisation
    • National Parks
  • Religious Studies and Philosophy
    • Belief in God
    • Sikhism
    • Islam Key Words
    • The Life Of Jesus
  • FNU
    • Keeping Safe in the Kitchen
    • Energy
  • Music
    • Listening
  • Computer Science
  • MFL
    • Spanish >
      • Grammar >
        • Verbs
      • Vocabulary
    • French >
      • Vocabulary
      • Grammar >
        • Verbs
  • How 2 Revise
  • Feedback
    • Have your say
    • Report an error
  • General Knowledge
  • Online Resources
  • Privacy
  • Scigo Starters
    • sgS Bio
  • Nouns and Adjectives
  • SciGoAs
    • A Level Mathematics >
      • Exponentials and Logarithms
      • Vectors
  • Science
    • Biology >
      • Reproduction
      • Blood and Circulation
      • Enzymes
      • The Skeleton and Muscles
      • Cells
      • Food and Digestion
      • Genetics
      • Respiration and Gas Exchange
      • Plant Metabolism - Photosynthesis and Respiration
      • Kidneys and Urinary system
      • Evolution
    • Chemistry >
      • Particles
      • Rocks
      • Atomic Structure and the Periodic Table
      • Rates of Reaction
      • Crude oil
      • Metals
      • Analysis
    • Physics >
      • Forces and Motion
      • Light and Sound
      • Energy Resources
      • Waves
      • Magnetism
  • Add Maths
    • Polynomials
    • Binomials
    • Permutations and Combinations
    • Graphs
  • Latin
    • Grammar >
      • Verbs
      • Participles
      • Nouns
      • Adjectives
    • Vocabulary
  • History
    • Battle of Hastings
    • World War Two and International Relations
    • Key Words- Medieval Era
    • The Tudor Church
    • The Spanish Armada
    • World War I
    • World War Two and International Relations
    • Why had international peace collapsed by 1939?
    • Glossary
  • Geography
    • The UK and The Republic of Ireland
    • Geographical Skills >
      • Ordnance Survey
    • UK Economy
    • Development
    • Globalisation
    • National Parks
  • Religious Studies and Philosophy
    • Belief in God
    • Sikhism
    • Islam Key Words
    • The Life Of Jesus
  • FNU
    • Keeping Safe in the Kitchen
    • Energy
  • Music
    • Listening
  • Computer Science
  • MFL
    • Spanish >
      • Grammar >
        • Verbs
      • Vocabulary
    • French >
      • Vocabulary
      • Grammar >
        • Verbs
  • How 2 Revise
  • Feedback
    • Have your say
    • Report an error
  • General Knowledge
  • Online Resources
  • Privacy
  • Scigo Starters
    • sgS Bio
  • Nouns and Adjectives
  • SciGoAs
    • A Level Mathematics >
      • Exponentials and Logarithms
      • Vectors