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
' create a variable called name
Dim name As String
name = "Bob"
' display name
MsgBox(name)
# create a variable called name
name = "Bob"
# display name
print(name)
name is string
set name = "Bob"
output name
// create a variable called name
string name = "Bob";
// display name
Console.WriteLine(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
' create a variable called score
Dim score As Integer
score = 10
' display score
MsgBox(score)
# create a variable called score
score = 10
# convert score to a string then display it
print(str(score))
score = 10
print(str(score))
score is integer
set score = 10
output score
// create a variable called score
int score = 10;
// convert score to a string then display it
Console.WriteLine(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"
int score = 10;
string name = "Bob";
Console.WriteLine(name + " has " + score + " points");
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.
SEND "Enter your name" To DISPLAY
RECEIVE name FROM (STRING) KEYBOARD
' Prompt the user to enter some text
Dim name As String
name = InputBox("Enter your name")
# Prompt the user to enter some text
name = input("Enter your name")
{ not specified in eduqas pseudocode reference }
// Prompt the user to enter some text
Console.WriteLine("Enter your name");
string name = Console.ReadLine();
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 }
Console.WriteLine("Enter your name");
string name = Console.ReadLine();
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 }
Console.WriteLine("How old are you?");
int name = int.Parse(Console.ReadLine());
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 }
// add this to the top of your file
using System.IO;
// Open the file
using(StreamWriter sw = new StreamWriter("output.txt")) {
// write to the file
sw.WriteLine("Hello world");
}
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")
# loop from 1 to 10
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 }
// add this to the top of your file
using System.IO;
// open the file
using(StreamWriter sw = new StreamWriter("numbers.txt")) {
// loop from 1 to 10
for(int num = 0; num < 10; num++) {
// write number to the file
sw.WriteLine(num);
}
}
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")
# loop from 1 to 10
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 }
// add this to the top of your file
using System.IO;
// open the file
using(StreamWriter sw = new StreamWriter("timestables.csv")) {
// lop from 1 to 10
for(int num = 1; num < 10; num++){
// write to the file
sw.Write(num);
sw.Write(",");
sw.Write(num*3);
sw.Write("\n");
}
}
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 }
// add this to the top of your file
using System.IO;
using(StreamReader sr = new StreamReader("data.txt")) {
string contents = sr.ReadToEnd();
}