# A Level Computer Science Programming Guide/Printable version

The current, editable version of this book is available in Wikibooks, the open-content textbooks collection, at
https://en.wikibooks.org/wiki/A_Level_Computer_Science_Programming_Guide

# Introduction

This Wikibook is not designed to teach you how to program, but rather to help you to remember and easily translate your code to the different languages.
All Pseudocode in this guide is based on various Cambridge Mark Schemes and Help Guides, so it is as accurate as possible.
When complete, the High-Level languages covered in this guide will be those that are set in the Cambridge syllabus; VB.Net, Python and Pascal/Delphi.

# Variables, Constants and Data Types
## Data types and their usage

Pseudocode :
| Data Type | Description | Usage | Example |
| INTEGER | A whole number | Uses the normal denary system | 123, -123, 0 |
| REAL | Any number that has fractional parts/decimals | Written with at least one digit either side of a decimal point | 12.4, 3.0, -17.34 |
| CHAR | A single character | A single character enclosed between single quotation marks | 'a', '@', '5' |
| STRING | A series of zero or more characters | No, or a series of characters enclosed between double quotation marks | "banana", "Q173hf", "John Doe" |
| BOOLEAN | A logical Expression | True or False | TRUE, FALSE |
| DATE | Any valid date | Use the format: dd/mm/yyyy If a different format must be used, use a comment to explain your decision. | 21/10/2015, 03/07/1985, 12/11/1955 |

## Variable Declarations

Python has no variable declarations.
| Language | General Usage | Example Usage |
| Pseudocode | DECLARE <Identifier> : <Data Type> | DECLARE Weight : INTEGER DECLARE Mass : REAL DECLARE Material : STRING |
| VB.NET | Dim <Identifier> As <Data Type> | Dim Weight As Integer Dim Mass As Double Dim Material As String |

## Constants

Python has no constants.
| Language | General Usage | Example Usage |
| Pseudocode | CONSTANT <Identifier> = <Value> | CONSTANT Pi = 3.14 CONSTANT e = "2.718" CONSTANT Multiplier = 10 |
| VB.NET | Const <Identifier> As <Data Type> = <Value> | Const Pi As Double = 3.14 Const e As String = "2.17" Const Multiplier as Integer = 10 |

## Assignments

| Language | General Usage | Example Usage |
| Pseudocode | <Identifier> ← <Value> | Name ← "Stephen" Age ← 47 Weight ← 19.32 |
| VB.NET | <Identifier> = <Value> | Name = "Stephen" Age = 47 Weight = 19.32 |
| Python | <Identifier> = <Value> | Name = "Stephen" Age = 47 Weight = 19.32 |

# Arrays
## Declaring Arrays

In Pseudocode, Arrays all have declarable Upper and Lower bounds.
In VB.NET, Arrays all have declarable Upper Bounds, but Lower Bounds are always 0.

### One Dimensional Arrays

| Language | General Usage | Example Usage |
| Pseudocode | DECLARE <Identifier> : ARRAY[<Lower Bound>:<Upper Bound>] OF <Data Type> | DECLARE NameList : ARRAY[1:5] OF STRING DECLARE YearlyRainfall : ARRAY[1900:2100] OF REAL DECLARE AgeList : ARRAY[0:40] OF INTEGER |
| VB.NET | Dim <Identifier>(<Upper Bound>) As <Data Type> | Dim NameList(4) As String Dim YearlyRainfall(200) As Double Dim AgeList(40) As Integer |
| Python | <Identifier> = | NameList = YearlyRainfall = AgeList = |

### Two Dimensional Arrays

| Language | General Usage | Example Usage |
| Pseudocode | DECLARE <Identifier> : ARRAY[<Lower Bound>:<Upper Bound>, <Lower Bound>:<Upper Bound>] OF <Data Type> | DECLARE GameArea : ARRAY[1:32, 0:16] OF STRING DECLARE SudokuGrid : ARRAY[1:9, 1:9] OF INTEGER DECLARE PopulationDensity : ARRAY[1:50, 20:50] OF REAL |
| VB.NET | Dim <Identifier>(<Upper Bound>, <Upper Bound>) As <Data Type> | Dim GameArea(31, 16) As String Dim SudokuGrid(8) As Integer Dim PopulationDensity(49,30) As Double |
| Python | <Identifier1> = <Identifier2> = <Identifier1>.append(<Identifier2>) | Game = GameArea = GameArea.append(Game) SudokuX = SudokuGrid = SudokuGrid.append(SudokuX) Longitude = PopulationDensity = PopulationDensity.append(Longitude) |

## Using Arrays
### One Dimensional Arrays

| Language | General Usage | Example Usage |
| Pseudocode | <Identifier>[<Index>] ← <Value> | NameList[1] ← "Stephen" YearlyRainfall[1985] ← 13.73 AgeList[39] ← 17 |
| VB.NET | <Identifier>(<Index>) = <Value> | NameList(0) = "Stephen" YearlyRainfall(85) = 13.73 AgeList(38) = 17 |
| Python | <Identifier>.append(<Value>) | NameList.append("Stephen") YearlyRainfall.append(13.73) AgeList.append(17) |

### Two Dimensional Arrays

| Language | General Usage | Example Usage |
| Pseudocode | <Identifier>[<Index>,<Index>] ← <Value> | GameArea[16, 5] ← "Fire" SudokuGrid[9, 3] ← 7 PopulationDensity[14, 32] ← 242.023 |
| VB.NET | <Identifier>(<Index>,<Index>) = <Value> | GameArea(15, 5) = "Fire" SudokuGrid(8, 2) = 7 PopulationDensity(13, 12) = 242.023 |
| Python | <Identifier1> = <Identifier2> = <Identifier1>.append(<Identifier2>) | Game.append("Fire") GameArea.append(Game) SudokoX.append(7) SudokuGrid.append(SudokuX) Longitude.append(242.023) PopulationDensity.append(Longitude) |

# User-Defined Data Types

Warning: Display title "User-Defined Data Types" overrides earlier display title "Variables, Constants and Data Types".

## Enumerated Data Types
### Declaring Enumerated Data Types

| Language | General Usage | Example Usage |
| Pseudocode | TYPE <Identifier> = (<Value 1>, <Value 2>, <Value 3>, ...) | TYPE Season = (Summer, Autumn, Winter, Spring) TYPE Direction = (North, North_East, East, South_East, South, South_West, West, North_West) TYPE SpiceIntensity = (No_Preference ,None, Mild, Medium, Hot, Extreme) |
| VB.NET | Enum <Identifier> <Value 1> = <Reference> <Value 2> = <Reference> <Value 3> = <Reference> .... End Enum | Enum Season Summer = 1 Autumn = 2 Winter = 3 Spring = 4 End Enum Enum SpiceIntensity None = 0 Mild = 1 Medium = 2 Hot = 3 Extreme = 4 End Enum |

### Using Enumerated Data Types

| Language | General Usage | Example Usage |
| Pseudocode | DECLARE <Identifier> : <Data Type> <Identifier> ← <Value> | DECLARE Seasons : Season Seasons ← Autumn DECLARE CompassPointer : Direction CompassPointer ← North DECLARE SpicePreference : SpiceIntensity SpicePreference ← Extreme |
| VB.NET | Dim <Identifier> As <Data Type> <Integer Variable> = <Identifier>.<Value> | Dim Seasons As Season SeasonCounter = Seasons.Autumn Dim CompassPointer As Direction CurrentDirection = CompassPointer.North Dim SpicePreference As SpiceIntensity SpiceCounter = SpicePreference.Extreme |

## Pointer Data Types

There is no VB.Net equivalent for a Pointer Data Type.
| Language | General Usage | Example Usage |
| Pseudocode | TYPE <Pointer> = ^<Type Name> | Type AddPointer = ^INTEGER Type NextLocation = ^STRING Type Pass = ^REAL |

## Record Data Types

| Language | General Usage | Example Usage |
| Pseudocode | TYPE <Identifier> DECLARE <Identifier> : <Data Type> DECLARE <Identifier> : <Data Type> ... END TYPE | TYPE Person DECLARE Name : STRING DECLARE Birthday : DATE DECLARE Weight : REAL DECLARE Address : STRING DECLARE SpicePreference : SpiceIntensity END TYPE |
| VB.NET | Structure <Identifier> Dim <Identifier> As <Datatype> Dim <Identifier> As <Datatype> ... End Structure | Structure Person Dim Name As String Dim Birthday As Date Dim Weight As Double Dim Address As String Dim SpicePreference As SpiceIntensity End Structure |

# Common Operations

Warning: Display title "Common Operations" overrides earlier display title "User-Defined Data Types".

## Input And Output

| Language | General Usage | Example Usage |
| Pseudocode | INPUT <Identifier> | Input Answer Input Age Input DistanceTravelled |
| VB.NET | <Identifier> = Console.Readline Dim <Identifier> As <Data Type> = Console.ReadLine | Answer = Console.ReadLine Dim Age As Integer = Console.ReadLine DistanceTravelled = Console.ReadLine |

## Arithmetic, Relational and Logic Operators

| Language | Arithmetic Operators | Arithmetic Operators | Arithmetic Operators | Arithmetic Operators | Integer Division | Integer Division | Relational Operators | Relational Operators | Relational Operators | Relational Operators | Relational Operators | Relational Operators | Logic Operators | Logic Operators | Logic Operators |
| Language | Addition | Subtraction | Multiplication | Division | Modulus | Division | Greater Than | Less Than | Greater Than or Equal to | Less Than or Equal to | Equal To | Not Equal To | AND | OR | NOT |
| Pseudocode | + | - | * | / | MOD | DIV | > | < | >= | <= | = | <> | AND | OR | NOT |
| VB.NET | + | - | * | / | MOD | \ | > | < | >= | <= | = | <> | AND | OR | NOT |

## String Operators

| Language | General Usage | Example Usage |
| Pseudocode | | |
| VB.NET | | |

## Random Number Generation

| Language | General Usage | Example Usage |
| Pseudocode | RANDOMBETWEEN(<Minimum>, <Maximum>) Generates a random INTEGER between the Minimum and Maximum.RND Generates a random REAL number between 0 and 1. | DECLARE Random : INTEGER Random = RANDOMBETWEEN(10, 17) DECLARE Random : REAL Random = RND |
| VB.NET | <Variable> = <Minimum> + Rnd * (<Maximum> - <Minimum>) Generates a random INTEGER between the Minimum and Maximum.Rnd Generates a random REAL number between 0 and 1. | Dim Random As Integer Random = RANDOMBETWEEN(10, 17) Random = 10 + Rnd * (17 - 10) Dim Random As Double Random = Rnd |

# Selection

Warning: Display title "Selection" overrides earlier display title "Common Operations".

## IF Statements

| Language | General Usage | Example Usage |
| Pseudocode | / If...Then Statement IF <Condition> THEN <Statements> ENDIF / If...Then...Else Statement IF <Condition> THEN <Statements> ELSE <Statements> ENDIF / ElseIf Statement IF <Condition> THEN <Statements> ELSEIF <Condition> THEN <Statements> ELSE <Statements> ENDIF | / If...Then Statement IF Age < 16 THEN CanDrive ← FALSE ENDIF / If...Then...Else Statement IF IsGreen THEN OUTPUT "Green" ELSE OUTPUT "Blue" ENDIF / ElseIf Statement IF Score > 1000 THEN Multiplier ← 10 ELSEIF Score < 500 THEN Multiplier ← 0 ELSE Multiplier ← 5 ENDIF |
| VB.NET | 'If...Then Statement If <Condition> Then <Statements> End If 'If...Then...Else Statement If <Condition> Then <Statements> Else <Statements> End If 'Else If Statement If <Condition> Then <Statements> Else If <Condition> Then <Statements> Else <Statements> End IF | 'If...Then Statement If Age < 16 Then CanDrive = False End If 'If...Then...Else Statement If IsGreen Then Console.WriteLine("Green") Else Console.WriteLine("Blue") End If 'Else If Statement If Score > 1000 Then Multiplier = 10 Else If Score < 500 Then Multiplier = 0 Else Multiplier = 5 End If |

## Nested IF Statements

| Language | General Usage | Example Usage |
| Pseudocode | IF <Condition> THEN IF <Condition> THEN <Statements> ENDIF ENDIF | IF Found = False THEN IF Searching = false THEN CALL Search ENDIF ENDIF IF Age >= 16 THEN IF PassedLicenceTest = True THEN CALL GenerateLicence ELSEIF TakenTest = False THEN OUTPUT "You need to take the test to get your licence" ELSE OUTPUT "You Failed Your Test" OUTPUT "You need to pass the test to get your licence" ENDIF ELSE OUTPUT "Ineligible for Licence" ENDIF |
| VB.NET | If <Condition> Then If <Condition> Then <Statements> End If End If | If Found = False Then If Searching = false Then Search End If End If If Age >= 16 Then If PassedLicenceTest = True Then GenerateLicence Else If TakenTest = False Then Console.WriteLine("You need to take the test to get your licence") Else Console.WriteLine("You Failed Your Test") Console.WriteLine("You need to pass the test to get your licence") End If Else Console.WriteLine("Ineligible for Licence") End If |

## CASE Statements

| Language | General Usage | Example Usage |
| Pseudocode | CASE OF <identifier> <value 1> : <statement> <value 2> : <statement> ... ENDCASE CASE OF <identifier> <value 1> : <statement> <value 2> : <statement> ... OTHERWISE <statement> ENDCASE | / If...Then Statement IF Age < 16 THEN CanDrive ← FALSE ENDIF / If...Then...Else Statement IF IsGreen THEN OUTPUT "Green" ELSE OUTPUT "Blue" ENDIF / ElseIf Statement IF Score > 1000 THEN Multiplier ← 10 ELSEIF Score < 500 THEN Multiplier ← 0 ELSE Multiplier ← 5 ENDIF |
| VB.NET | 'If...Then Statement If <Condition> Then <Statements> End If 'If...Then...Else Statement If <Condition> Then <Statements> Else <Statements> End If 'Else If Statement If <Condition> Then <Statements> Else If <Condition> Then <Statements> Else <Statements> End IF | 'If...Then Statement If Age < 16 Then CanDrive = False End If 'If...Then...Else Statement If IsGreen Then Console.WriteLine("Green") Else Console.WriteLine("Blue") End If 'Else If Statement If Score > 1000 Then Multiplier = 10 Else If Score < 500 Then Multiplier = 0 Else Multiplier = 5 End If |

# File Handling

Warning: Display title "File Handling" overrides earlier display title "Selection".

### Note for VB.NET users:

While the methods used here are still valid, they are generally not used. They have been included in this guide as they are more similar to the Pseudocode file management, hopefully making them easier to understand. Even though it is not practice to use them, they should still be acceptable in an exam.

## Handling Text Files
### Opening Text Files

| Language | General Usage | Example Usage |
| Pseudocode | OPENFILE <File Identifier> FOR <File Mode> | OPENFILE Beans.txt FOR APPEND OPENFILE Styles.css FOR READ OPENFILE Names.tmp FOR WRITE |
| VB.NET | FileOpen(<File Number>], <File Identifier>, OpenMode.<File Mode>) | FileOpen(17, “Beans.txt”, OpenMode.Append) FileOpen(FileNumber, “Styles.css”, OpenMode.Input) FileOpen(NamesFile, “Names.tmp”, OpenMode.Output) |
In VB.NET you can use the FreeFile function to automatically obtain an unused file number, avoiding any problems that could occur if accidentally using the same number for two files.
It is good practice to always keep track of your file numbers, as it will help reduce bugs (aka forgetting or using the wrong file number), remove any Magic Numbers and make the code easier to read and understand.
File Mode Usage :
| File Mode Identifiers | File Mode Identifiers | Description |
| Pseudocode | VB.NET | Description |
| APPEND | OpenMode.Append | Used when Writing data to the file. If the file already exists, the new data will be added to the file after any existing data. |
| READ | OpenMode.Input | Used when Reading data from the file. |
| Write | OpenMode.Output | Used when Writing data to the file. If the file already exists, the file will be deleted and a new file will be created with the new data. |

### Reading From Text Files

Once a file is opened in 'Read' mode, you can use the following commands to read the data from the files.
| Language | General Usage | Example Usage |
| Pseudocode | READFILE <File Identifier>, <Identifier> | /Datatypes declared as strings READFILE Styles.css, NextLine READFILE TransactionHistory.txt, Transaction READFILE Address.db, CurrentAddress WRITEFILE <File identifier>, <Variable> |
| VB.NET | <Identifier> = LineInput(<File Identifier>) The File Identifer is the file number used identify the file when it was opened. | NextLine = LineInput(StylesFileNumber) Transaction = LineInput(TransactionHistoryFileNumber) CurrentAddress = LineInput(AddressFileNumber) |
The Variable should be of data type STRING. This command reads the text file line by line.
The EOF function can be used in Pseudocode and VB.NET to determine whether the file pointer is at the end of the file. It returns a Boolean Value, and can be useful when implementing loops.
| Pseudocode | VB.NET |
| EOF(file.txt) | EOF(FileNumber) |

### Writing To Text Files

Once a file is opened in 'Write' or 'Append' mode, you can use the following commands to write the data to the files.
| Language | General Usage | Example Usage |
| Pseudocode | READFILE <File Identifier>, <Identifier> | WRITEFILE Beans.txt, BeanData WRITEFILE Names.tmp, CurrentUser WRITEFILE Recipes.db, NewRecipe |
| VB.NET | PrintLine(<File Identifier>, <Identifier>) The File Identifer is the file number used identify the file when it was opened. | PrintLine("Beans.txt", BeanData) PrintLine("Name.tmp", CurrentUser) PrintLine("Recipes.db" NewRecipe) |
The Variable should be of data type STRING.

### Closing Text Files

| Language | General Usage | Example Usage |
| Pseudocode | CLOSEFILE <File identifier> | CLOSEFILE Beans.txt CLOSEFILE Styles.css CLOSEFILE Names.tmp |
| VB.NET | FileClose(<File Identifier>) The File Identifer is the file number used identify the file when it was opened. | FileClose(17) FileClose(FileNumber) FileClose(NamesFile) |
You should always remember to close your files. Failure to do so can result in major problems later on when any program tries to open the file, or the program tries to open or edit a different file with the same identifier.

# Algorithms

Sorting
* Bubble Sort
* Insertion Sort
Searching
* Binary Sort
Listing
* Stacks
* Queues
* Linked Lists
* Binary Trees
* Hash Tables