Categories: All Variables and Constants  String manipulation  Builtin functions  Input and output  Arrays  Conditional logic  Repetition / Iteration  Functions / Procedures 

Show:

This resource is designed as a quick revision guide and must not be used by students during a NEA where internet access is not allowed. It has not been endorsed by any exam boards. If you spot any mistakes, please let me know and I'll fix them asap.

Input and output

The purpose of almost every program is to read data in from somewhere, process it into useful information and then send it out somewhere else.

Inputs can be the keyboard, mouse or reading from a file or network.

Outputs can be the screen, speakers or saving to a file or network.

Top Tips: 

  • Think about the type of data you're reading in and where you're going to store it.
    Integers are whole numbers; strings are text; booleans are either true or false and reals are numbers that can have a decimal point.
  • Think about what needs to be done to that data to process it into useful information.
    When you read a value from an input, you often need to save it into a variable so you can use it later. You can use operators (plus +, minus - , times * and divide /) to process integers and reals and you can use concatenation and substrings to process strings.
  • Think about the type of data you're sending out.
    See the first bullet point. It's also worth thinking about the order you want to output information as it will appear so you can plan the order of your lines of code.
Displaying text
    

Most programs need to output text to the user. 

This example displays the text "Hello" to the screen

SEND "Hello" TO DISPLAY
MsgBox("Hello")
print("Hello")
print("Hello")
output "Hello"
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.

STRING name SET name TO "Bob" SEND name TO DISPLAY
Dim name As String name = "Bob" MsgBox(name)
name = "Bob" print(name)
name = "Bob" print(name)
name is string set name = "Bob" output name
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.

INTEGER score SET score TO 10 SEND score TO DISPLAY
Dim score As Integer score = 10 MsgBox(score)
score = 10 print(str(score))
score = 10 print(str(score))
score is integer set score = 10 output score
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"

INTEGER score SET score TO 10 STRING name SET name TO "Bob" SEND name & " has " & score & " points"
Dim score As Integer score = 10 Dim name As String name = "Bob" MsgBox(name & " has " & score & " points")
score = 10 name = "Bob" print(name + " has " + str(score) + " points")
score = 10 name = "Bob" print(name + " has " + str(score) + " points")
score is integer set score = 10 name is string set name = "Bob" output name + " has " + score + " 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.

RECEIVE name FROM (STRING) KEYBOARD
Dim name As String name = InputBox()
name = input()
name = input()
{ not specified in eduqas pseudocode reference }
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.

SEND "What is your name?" TO DISPLAY RECEIVE name FROM (STRING) KEYBOARD
Dim name As String name = InputBox("What is your name?")
name = input("What is your name?")
name = input("What is your name?")
{ not specified in eduqas pseudocode reference }
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.

SEND "How old are you?" TO DISPLAY RECEIVE age FROM (INTEGER) KEYBOARD
Dim age As Integer age = InputBox("How old are you?")
name = int(input("How old are you?"))
name = int(input("How old are you?"))
{ not specified in eduqas pseudocode reference }
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 "output.txt" "Hello world"
My.Computer.FileSystem.WriteAllText("output.txt", "Hello world", False) ' Change the False to True if you want to ' append text to the end of the file ' rather than overwrite the file
# open the file f = open("output.txt", "w") # write to the file f.write("Hello world") # close the file so it can be read by other programs f.close()
// open the file f = openWrite("output.txt") // write to the file f.writeLine("Hello world") // close the file so it can be read by other programs f.close()
{ not specified in eduqas pseudocode reference }
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

FOR num FROM 1 TO 10 DO WRITE "numbers.txt", num END FOR
' Open the file numbers.txt Dim s As System.IO.StreamWriter s = My.Computer.FileSystem.OpenTextFileWriter("numbers.txt", False) ' Loop from 1 to 10 Dim num As Integer For num = 1 To 10 ' Write the number s.WriteLine(num) Next ' Close the file so it can be read by other programs s.Close()
# open the file f = open("numbers.txt", "w") for num in range(1, 10): # write number to the file f.write(num) # add blank line to file f.write("\n") # close the file so it can be read by other programs f.close()
// open the file f = openWrite("numbers.txt") for num = 1 to 10 // write number to the file f.writeLine(num) next num // close the file so it can be read by other programs f.close()
{ not specified in eduqas pseudocode reference }
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...

FOR num FROM 1 TO 10 DO WRITE "timestables.csv", num, num * 3 END FOR
' Open the file numbers.txt Dim s As System.IO.StreamWriter s = My.Computer.FileSystem.OpenTextFileWriter("timestables.csv", False) ' Loop from 1 to 10 Dim num As Integer For num = 1 To 10 ' Write the number, a comma, then the number * 3. All on separate lines s.Write(num) s.Write(",") s.Write(num * 3) s.Write(vbNewLine) Next ' Close the file so it can be read by other programs s.Close()
# open the file f = open("timestables.csv", "w") for num in range(1, 10): # write to the file f.write(num) f.write(",") f.write(num*3) f.write("\n") # close the file so it can be read by other programs f.close()
// open the file f = openWrite("timestables.csv") for num = 1 to 10 // write to the file f.writeLine(num + "," + num*3) next num // close the file so it can be read by other programs f.close()
{ not specified in eduqas pseudocode reference }
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

STRING contents READ "data.txt" contents
Dim contents as String contents = My.Computer.FileSystem.ReadAllText("data.txt")
f = open("data.txt") contents = f.read() f.close()
myFile = openRead("data.txt”) contents = "" while NOT myFile.endOfFile() contents = contents + myFile.readLine() + "\n" endwhile myFile.close()
{ Eduqas hasn't specified how to read from a text file in the pseudocode spec }

Loading...