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.
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.
Integers are whole numbers; strings are text; booleans are either true or false and reals are numbers that can have a decimal point.
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.
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.
Most programs need to output text to the user.
This example displays the text "Hello" to the screen
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.
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.
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"
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.
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.
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.
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"
An integer is a whole number (without any decimal places).
This example writes the numbers 1-10 into a file called numbers.txt
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...
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
Loading...