 ## Show:

This resource is designed as a quick reference or revision guide. It has not been endorsed by any exam boards. If you spot any mistakes, please let me know and I'll fix them asap.

## Introduction

This website aims to give a quick reference for VB.NET, Python, C# and pseudocode and is aimed primarily at teachers & students working towards a GCSE in Computer Science

VB.NET, Python and C# are programming languages designed to be understood and followed by computers. Pseudocode is not a programming language: it's written to be understood by humans so that they can turn it into any programming language.

Each exam board has published a document saying how they'll write pseudocode in their exams. The whole idea of syntax (a set of rules) for pseudocode is silly - it's not designed to be a programming language that is run by a computer. With this in mind, all exam boards state that you don't have to follow the syntax for 'their' version of pseudocode when you write out your own algorithms, but you should be able to understand their version of pseudocode when reading an algorithm in an exam.

### Top Tips:

• Explore the different categories of skills at the top of this page
Each skill has example code in VB.NET, Python, C# and Pseudocode for Edexcel, Eduqas and OCR GCSE.
• Search for a specific skill
If you know what you're looking for, use the search bar above the categories list.
Integer variables

Integers are whole numbers

Variables let you store data

Integer variables let you store whole numbers

In this example, a variable called score is declared then set to the integer value of 100

Real variables

Reals are numbers that can contain a decimal place.

Reals are also known as floats or floating point numbers because they may have a decimal point floating somewhere inside them.

Variables let you store data.

A real variable lets you store a number that can contain a decimal place.

In this example, a variable called height is declared then set to the real value of 6.2

Boolean variables

Boolean means one of two possible values: usually True or False.

Variables let you store data.

Boolean variables let you store the either value True or False, but nothing else

In this example, a variable called hungry is declared then set to True

Character variables

Characters are single letters or digits such as '1', 'H' or '?'

Variables let you store data.

Character variables let you store a single letter, number or punctuation mark.

In this example, a variable called letter is declared then set to a full stop.

Integer constants

Integers are whole numbers (with no decimal places).

Constants store a value that is set once and then never changes.

Integer constants let you give a whole number a name so you can use it to make your code easier to read or to set options for your code that the user wont be able to change.

In this example, a constant called NUMBER_OF_TRIES is set to the integer value of 10

Real constants

Reals are numbers that can contain a decimal place.

Constants store a value that is set once and then never changes.

Real constants let you give a decimal number a name so you can use it to make your code easier to read or to set options for your code that the user wont be able to change.

In this example, a constant called PI is set to the real value of 3.141

Boolean constants

Boolean means one of two values (usually either True or False).

Constants store a value that is set once and then never changes.

Boolean constants let you give a boolean value a name so you can use it to make your code easier to read or to set options for your code that the user wont be able to change.

In this example, a constant called CHEAT_MODE is set to the boolean value of True

Character constants

Characters are single letters or digits such as '1', 'H' or '?'

Constants store a value that is set once and then never changes.

Character constants let you give a character value a name so you can use it to make your code easier to read or to set options for your code that the user wont be able to change.

In this example, a constant called KEY_QUIT is set to the character value of 'Q'

String variables

A string is some text such as "Hello" or "Area 51"

Variables let you store data.

String variables let you store text.

In this example a variable called name is set to the string value of "Bob"

String constants

A string is some text such as "Hello" or "Area 51"

Constants store a value that is set once and then never changes.

String constants let you give some text data a name so that you can use it to make your code easier to read or set options for your code that the user wont be able to change.

In this example, a constant called ERROR_MESSAGE is set to the string value of "Don't panic!"

Concatenating strings

A string is some text such as "Hello"

Concatenating means joining together or adding / appending on to the end.

Concatenating strings means joining two strings together.

In this example, two strings (first_name and last_name) are concatenated (joined together) and saved in another string variable called full_name with a space in between them.

Length of a string

A string is some text such as "Hello"

The length of a string means how many characters (including spaces, numbers and punctuation) there are in that string

This example createa a string called name and sets it to "Bob". It counts how many characters are in that string and stores the answer (3) into a variable called l

Getting a character from a string

A string is some text such as "Hello"

A character is a single letter or digit.

Sometimes it's useful to get the 1st, 2nd or whichever character from a string.

This example gets the first character of a string called name and stores it into a variable called first_letter

Choose a random number

Computers can't think for themselves - they can only follow instructions., so getting them to choose a number that is truely random is not easy. Thankfully, most programming languages have a function that will choose a number for you that appears to be chosen at random.

This example generates a random integer between 0 and 10 and saves it into a variable called r

Comments don't affect how the code runs. They're there to explain the code so that the programmer (or someone else) can understand what the code does.

Well written comments describe what a line or section of code does. They make your code easier to maintain / extend / improve / debug in the future.

Displaying text

Most programs need to output text to the user.

This example displays the text "Hello" to the screen

Displaying a string variable

A string variable lets your program store text that might change as your program runs.

This example creates a string variable called name and displays it to the screen.

Displaying an integer variable

An integer variable lets your program store a whole number that might change as your program runs.

This example creates an integer variable called score and displays it to the screen.

Displaying a mixture of text and variables

Variables let your program store data of a particular data type.

Integer variables store whole numbers. String variables store text.

Sometimes you need to combine text with integer variables and string variables when you display it to the screen.

This example has a string variable called name (set to "Bob") and an integer variable called score (set to 10). It combines both variables with some text to display the message "Bob has 10 points"

Get user input

Most programs need to get the user to type in data that controls how the program behaves.

This program asks the user for their name and saves the result into a string variable called name.

Ask the user to enter a string

A string is some text that can contain more than one letter, number, space or punctuation mark.

This program asks the user their name and saves the result into a string variable called name.

Ask the user to enter an integer

An integer is a whole number.

This program asks the user to type in their age. Whatever the user types in is then saved into an integer variabled called age.

Write a string to a file

A string is some text that can contain more than one letter, number, space or punctuation mark.

This example writes the string "Hello world" to the file "output.txt"

Write an integer to a file

An integer is a whole number (without any decimal places).

This example writes the numbers 1-10 into a file called numbers.txt

Write data to a csv file

A csv file stands for a Comma-separated-values file. It will open as a spreadsheet (often in Excel) with lots of data organised into rows and columns.

When you open the csv file in a text editor (like notepad), the value for each cell is separated by a comma. Each row is written on a new line.

This example writes the 3 times table to a file called "timestables.csv":

e.g:

1, 3

2, 6

3, 9

4, 12

etc...

Create array of strings

An array let your store more than one piece of data into the same variable as an ordered list.

This example stores three strings into the array called names.

Create array of numbers

An array lets you store more than one piece of data into the same variable as an ordered list.

This example stores three numbers into the array called ages.

Access data in an array

An array lets you store more than one piece of data into the same variable as an ordered list.

Data in an array can be accessed using an index. This is a number which defines the position in the list, which starts counting from 0

This example displays the first string in an array called names

Create a 2D array

If an array is like a list of data, a 2d array is like a grid of data where the data is stored in one variable but organised into rows and columns like a spreadsheet.

This example stores details of 3 different fruit for a supermarket.

It will then display the colour of the first fruit.

The first index will select the row then the second index selects the column

IF statement

If you want your code to make a decision you'll need an IF statement.

This example asks the user how old they are and checks that they've entered a number bigger than 0

IF ... ELSE statement

If you want your code to choose between two possibilities you'll need an IF ... ELSE statement.

This example asks the user what their name is. If they answer the question it says hello. If they don't answer the question it displays an error message.

Not equal to

You can test if two expressions are the same using the equal to or not equal to operator.

An expression can be a variable or combination of variables & data (e.g. 1 + 1)

An operator is a symbol that means add, subtract or something similar.

This example asks the user to enter a temperature  of a bath (in degrees Celsius) and then follows the following rules:

Equal to 37 degrees: says "Perfect - that's body temperature"

Not equal to 37 degrees: "Not body temperature"

Greater than or less than

If you need to compare two expressions you will need the greater than or less than operators.

An expression can be a variable or combination of variables & data (e.g. 1 + 1)

An operator is a symbol that means add, subtract or something similar.

This example asks the user to enter a password and displays how strong that password is based purely on the length:

Less than 5 characters: weak

More than or equal to 8 characters: strong

Otherwise: medium

While loop

A while loop will keep repeating whilst a condition is met (it will stop repeating when the condition is no longer met).

This is known as a pre-conditioned loop because the program checks if the condition has been met before the code in the loop runs. This means that the minimum number of times the loop will run could be 0.

It is also an example of indefinite iteration because the loop could carry on forever unless the condition is met.

This example program keeps asking the user "Are we nearly there yet" until they say "yes"

Repeat Until loop

A repeat until loop will keep repeating until a condition is met.

This is known as a post-conditioned loop because the program checks if the condition has been met after the code in the loop runs. This means that the loop will always run at least once.

It is also an example of indefinite iteration because the loop could carry on forever if the condition is never met.

This example program keeps asking the user "Are we nearly there yet?" until they say "yes"

Repeat ... times

Repeating ... number of times is known as a count controlled loop because you specify exactly how many times the loop will repeat.

Repeat ... times is an example of definite iteration because the number of times the loop repeats is clearly defined.

This example program will repeat the word "Why?" 10 times

For loop

A for loop is a count-controlled loop which means it your code says exactly how many times the loop should repeat.

A for loop will use a variable to count between the minimum and maximum value. This variable is called the index, which is why for loops often use the variable i.

A for loop is an example of definite iteration because the number of times the loop repeats is clearly defined.

This example will count from 10 to 20

For loop (with step)

for loop is a count-controlled loop which means it your code says exactly how many times the loop should repeat.

A for loop will use a variable to count between the minimum and maximum value. This variable is called the index, which is why for loops often use the variable i.

Rather than increasing the index by 1 each time, you can step up by any amount until you reach the maximum.

This example will count down from 10 to 1 then say "Blast off!"

For Each loop

A for each loop means the code in the loop will run once for each item in an array.

This means it is a count-controlled loop because the number of times the loop will run is not dependent on a condition being met: you know how many times it will repeat in advance.

A for each loop is an example of definite iteration because the number of times the loop repeats is clearly defined.

This example will display the lyrics for "The wheels on the ... go round and round"  for an array containing the strings "bus", "car" and "tram"

Create and use a procedure

A procedure is a section of code that has a name that you can re-use to do something useful.

A procedure definition is the code that tells the computer what to do whenever that procedure runs.

The procedure wont actually do anything until you call it.

This example defines a procedure that shows a random word then calls it twice.

Create and use a procedure with parameters

A procedure is a section of code that has a name, that you can re-use to do something useful.

Parameters are variables that allow you to send data to the procedure to customise what it does or how it works.

A procedure definition is the code that tells the computer what to do whenever that procedure runs. The parameters are the variables named in brackets after the procedure name.

The procedure wont actually do anything until you call it.

This example defines a procedure which will display a random number. It has two parameters which allow you to set the minimum and maximum number the random number will be between.

This procedure is called to show a random number between 10 and 20 and then called again to choose a random number between 50 and 100.

Create and use a function

A function is like a procedure except that it always returns a value.

This return value is calculated when the function is called and can be saved into a variable or used later in the program.

Both functions and procedures are sections of code that have been given a name, which can be re-used to do something useful.

This example defines a function which chooses a random word. This function is called twice with the return value being stored into separate variables.

Create and use a function with parameters

A function is like a procedure except that it always returns a value.

Parameters are variables that allow you to send data to the function to customise what it does or how it works.

The parameters are the variables named in brackets after the function name.

This return value is calculated when the function is called and can be saved into a variable or used later in the program.

Both functions and procedures are sections of code that have been given a name, which can be re-used to do something useful.

This example defines a function which will generate and return a random number. It has two parameters which allow you to set the minimum and maximum number the random number will be between.

This function is called to choose a random number between 10 and 20 and then called again to choose a random number between 50 and 100. Both numbers are returned and saved into separate variables.

Casting (converting) to a string

Often it's useful to convert a number (integer or real data type) to text (string data type) so that it can be processed or sent as an output later in the program.

This example converts the integer number 1000 to a string containing "1000"

Casting (converting) to an integer

Often it's useful to round a decimal number (real data type) to a whole number (integer data type).

It can also be useful to convert a string representation of a number (e.g. one typed in by the user) to an integer

This example converts the string "1000" to the integer 1000

Casting (converting) to a real

Often it's useful to convert a string representation of a number (e.g. one typed in by the user) to a decimal number for use in calculations (real data type)

This example converts the string "1.1234" to the real value 1.1234

Read all data from a text file

The easiest way to read data from a file is to get the whole file contents as a string.

This example loads the contents of the file "data.txt" into a string variable called contents

Create a list of strings

A list is similar to an array: both allow more than one piece of data to be stored in the same variable.

It's not possible to add or remove data to an array after it's been set for the first time but it is possible to add or remove data to a list.

This example creates a list called colours and adds three strings into it before displaying the first item in the list.

Create a list of integers

list is similar to an array: both allow more than one piece of data to be stored in the same variable.

It's not possible to add or remove data to an array after it's been set for the first time but it is possible to add or remove data to a list.

This example creates a list called numbers and adds three integer numbers into it before displaying the sum of all the numbers added together.

Remove data from a list

list is similar to an array: both allow more than one piece of data to be stored in the same variable.

It's not possible to add or remove data to an array after it's been set for the first time but it is possible to add or remove data to a list.

This example creates a list of strings called names. It adds 3 names to the list then removes the first one.

Splitting a string into an array of strings

It can be really useful to split one string into lots of smaller parts whenever you see a particular character.

This example will split the string "red,green,blue" into an array of smaller strings: "red", "green" and "blue"

It will then display the first colour: red

Getting part of a string (substring)

A string is some text. A substring is part of a bigger string. For example, "name" is a substring of "Hello, what's your name?".

This example creates a variable containing the string "What is your name" then displays the substring of the 5th and 6th characters ("is")