Show:

This resource is designed as a syntax guide that can be used offline as a printable resource in an Edexcel Computer Science NEA.

Syntax Guide

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

VB.NET and Python are both 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.

Skill
Description
VB.NET
Python
Data types
  

Integers are whole numbers

Reals are numbers that can have decimal places

Booleans are either True or False

Character is a single digit

Strings are made up of characters

INTEGER Counter SET Counter TO 0 REAL r SET r TO 3.14 BOOLEAN b SET b TO TRUE CHARACTER c SET c TO "!" STRING MyString SET MyString TO "Hello world"
Dim Counter As Integer i = 0 Dim r As Double r = 3.14 Dim b As Boolean b = True Dim c as Char c = "!" Dim MyString as String MyString = "Hello World"
counter = 0 r = 3.14 b = True c = "!" my_string = "Hello World"
Constants
  

The value of constants can only ever be set once. 

CONST REAL PI SET PI TO 3.14
Const PI as Double = 3.14
PI = 3.14
Arrays
  

Initialises a one-dimensional array with a set of values.

Indices start at zero (0) for all data structures.

SET ArrayValues TO [1, 2, 3, 4, 5]
Dim ArrayValues() As Integer ArrayValues = {1, 2, 3, 4, 5}
array_values = [1, 2, 3, 4, 5]
Length of array
  

Count the number of values in an array

LENGTH(array)
array.Length
len(array)
Length of string
  

Count the number of characters in a string

LENGTH(string)
string.Length
len(string)
Random numbers
  

This generates a random number from 0 to n.

RANDOM(n)
Randomize() Rnd() * n
import random random.randint(0, n)
Comments
  

A comment can be on a line by itself or at the end of a line.

# Comments are indicated by the # symbol, followed by any text.
' Comments are indicated by the ' symbol, followed by any text.
# Comments are indicated by the # symbol, followed by any text.
Set a value in an array
  

Assigns a value to an element of a one-dimensional array.

Indices start at zero (0) for all data structures.

SET ArrayClass[1] TO "Ann" SET ArrayMarks[3] TO 56
ArrayClass(1) = "Ann" ArrayMarks(3) = 56
array_class(1) = "Ann" array_marks(3) = 56
Set a value in a 2D array
  

Assigns a value to an element of a two dimensional array.

Indices start at zero (0) for all data structures.

SET ArrayClassMarks[2,4] TO 92
ArrayClassMarks(2,4) = 92
array_class_marks[2,4] = 92
IF
  

If <expression> is true then command is executed.

IF Answer = 10 THEN SET Score TO Score + 1 END IF
If Answer = 10 Then Score = Score + 1 End If
if answer == 10: score += 1
IF ... ELSE
  

If <expression> is true then first <command> is executed, otherwise second <command> is executed.

IF Answer = "correct" THEN SEND "Well done" TO DISPLAY ELSE SEND "Try again" TO DISPLAY END IF
If Answer = "correct" Then MsgBox("Well done") Else MsgBox("Try again") End If
if answer == "correct": print("Well done") else: print("Try again")
WHILE loop
  

Pre-conditioned loop. Executes <command> whilst <condition> is true.

WHILE Flag = 0 DO SEND "All well" TO DISPLAY END WHILE
While Flag = 0 MsgBox("All well") End While
while flag == 0: print("All well")
REPEAT UNTIL loop
  

Post-conditioned loop. Executes <command> until <condition> is true. The loop must execute at least once.

REPEAT SET Go TO Go + 1 UNTIL Go = 10
Do Go = Go + 1 Loop Until Go = 10
while True: go += 1 if go == 10: break
REPEAT ... TIMES loop
  

Count controlled loop. The number of times <command> is executed is determined by the expression

REPEAT 100-Number TIMES SEND "*" TO DISPLAY END REPEAT
For i as Integer = 0 To 100 MsgBox("*") Next
for i in range(100): print("*")
FOR loop
  

Count controlled loop. Executes a fixed number of times.

FOR Index FROM 1 TO 10 DO SEND ArrayNumbers[Index] TO DISPLAY END FOR
For Index as Integer = 1 To 10 Msgbox(ArrayNumbers(Index)) Next
for index in range(1,10): print(array_numbers[index])
FOR loop with step
  

Count controlled loop using a step

FOR Index FROM 1 TO 500 STEP 25 DO SEND Index TO DISPLAY END FOR
For Index as Integer = 1 To 500 Step 25 MsgBox(Index) Next Index
for index in range(1, 500, 25): print(index)
FOR EACH loop
  

Count controlled loop. Executes for each element of an array.

SET WordsArray TO ["The"’, "Sky", "is", "grey"] SET Sentence to "" FOR EACH Word FROM WordsUArray DO SET Sentence TO Sentence & Word & " " END FOREACH
Dim WordsArray() As String WordsArray = {"The", "Sky", "is", "grey"} Dim Sentence As String = "" For Each Word As String In WordsArray Sentence = Sentence & Word & " " Next
words_array = ["The", "Sky", "is", "grey"] sentence = "" for word in words_array: sentence += word + " "
Display text on screen
  

Sends output to the screen.

SEND "Have a good day." TO DISPLAY
MsgBox("Have a good day")
print("Have a good day")
Read input from user
  

Reads input of specified type.

RECEIVE Name FROM (STRING) KEYBOARD RECEIVE LengthOfJourney FROM (INTEGER) KEYBOARD RECEIVE YesNo FROM (CHARACTER) KEYBOARD
Dim Name as String Name = InputBox("Enter your name") Dim LengthOfJourney As Integer LengthOfJourney = InputBox("How long?") Dim YesNo As Char YesNo = InputBox("Y or N?")
name = input("Enter your name") length_of_journey = input("How long?") yes_no = input("Y or N?")
Read one line from a file
  

Reads in a record from a <file> and assigns to a <variable>.

Each READ statement reads a record from the file.

READ MyFile.doc Record
Dim file = My.Computer.FileSystem.OpenTextFileReader("MyFile.doc") Dim Line as String = file.ReadLine() Dim Records() As String = Line.split(",") Record = Records(0) file.Close()
f = open("MyFile.doc") line = f.readline() records = line.split(",") record = record[0] f.close()
Read whole contents of file
  

Reads in the whole contents from a <file> and assigns to a <variable>.

READ ALL MyFile.doc Contents
Dim Contents As String Contents = My.Computer.FileSystem.ReadAllText("MyFile.doc")
f = open("MyFile.doc") record = f.read() f.close()
Write to a file
  

Writes a record to a file.

Each WRITE statement writes a record to the file.

WRITE MyFile.doc Answer1, Answer2, "xyz 01"
Dim file = My.Computer.FileSystem.OpenTextFileWriter("MyFile.doc", False) file.Write(Answer1 & ",") file.Write(Answer2 & ",") file.Write("xyz 01" & vbNewLine) file.Close()
f = open("MyFile.doc", "w") f.write(answer1 + ",") f.write(answer2 + ",") f.write("xyz 01 + "\n") f.close()
Define a procedure
  

Defines a procedure.

PROCEDURE CalculateAverage (Mark1, Mark2, Mark3) BEGIN PROCEDURE SET Avg TO (Mark1 + Mark2 + Mark3)/3 END PROCEDURE
Sub CalculateAverate(Mark1, Mark2, Mark3) Dim Avg = (Mark1 + Mark2 + Mark3) / 3 MsgBox(Avg) End Sub
def calculate_average(mark1, mark2, mark3): avg = (mark1 + mark2 + mark3) / 3 print(avg)
Define a function
  

Defines a function.

FUNCTION AddMarks (Mark1, Mark2, Mark3) BEGIN FUNCTION SET Total to Mark1 + Mark2 + Mark3 RETURN Total END FUNCTION
Function AddMarks(Mark1, Mark2, Mark3) Dim Total Total = Mark1 + Mark2 + Mark3 Return Total End Function
def add_marks(mark1, mark2, mark3): total = mark1 + mark2 + mark3 return total
Call a procedure or function
  

Calls a procedure or a function.

ShowTotal(FirstMark, SecondMark) Total = CalculateTotal(FirstMark, SecondMark)
ShowTotal(FirstMark, SecondMark) Total = CalculateTotal(FirstMark, SecondMark)
show_total(first_mark, second_mark) total = calculate_total(first_mark, second_mark)
Arithmetic operators
  

Used to change values by performing mathematical calculations.

 

# Add (=2) 1 + 1 # Subtract (=3) 4 - 1 # Divide (=2.5) 5 / 2 # Multiply (=6) 2 * 3 # Exponent (=8) 2 ^ 3 # Modulo (=1) 5 MOD 2 # Integer division (=2) 5 DIV 2
' Add (=2) 1 + 1 ' Subtract (=3) 4 - 1 ' Divide (=2.5) 5 / 2 ' Multiply (=6) 2 * 3 ' Exponent (=8) 2 ^ 3 ' Modulo (=1) 5 Mod 2 ' Integer division (=2) 5 \ 2
# Add (=2) 1 + 1 # Subtract (=3) 4 - 1 # Divide (=2.5) 5 / 2 # Multiply (=6) 2 * 3 # Exponent (=8) 2 ** 3 # Modulo (=1) 5 \\ 2 # Integer division (=2) 5 DIV 2
Relational operators
  

Used to compare two values

# True if v1 equals v2 v1 = v2 # True if v1 isn't equal to v2 v1 <> v2 # True if v1 is greater than v2 v1 > v2 # True if v1 is greater than or equal to v2 v1 >= v2 # True if v1 is less than v2 v1 < v2 # True if v1 is less than or equal to v2 v1 <= v2
' True if v1 equals v2 v1 = v2 'True if v1 isn't equal to v2 v1 <> v2 'True if v1 is greater than v2 v1 > v2 ' True if v1 is greater than or equal to v2 v1 >= v2 ' True if v1 is less than v2 v1 < v2 ' True if v1 is less than or equal to v2 v1 <= v2
# True if v1 equals v2 v1 == v2 # True if v1 isn't equal to v2 v1 != v2 # True if v1 is greater than v2 v1 > v2 # True if v1 is greater than or equal to v2 v1 >= v2 # True if v1 is less than v2 v1 < v2 # True if v1 is less than or equal to v2 v1 <= v2
Logical operators
  

Used to combine multiple boolean expressions:

AND: Returns true if both conditions are true.

OR: Returns true if any of the conditions are true.

NOT: Reverses the outcome of the expression; true becomes false, false becomes true.

<expression1> AND <expression2> <expression1> OR <expression2> NOT <expression1>
<expression1> And <expression2> <expression1> Or <expression2> Not <expression1>
<expression1> and <expression2> <expression1> or <expression2> not <expression1>

Loading...