Variables, constants, data types, input and output
Every program stores values while it runs. This topic covers naming them, choosing the right data type, and getting values in and out of a program.
Variables and constants
A variable is a named store whose value can change while the program runs. A constant is a named value that is set once and can’t change. Using a constant, such as a tax rate, means the value is written in one place: to change it you edit one line, and the program can’t change it by mistake.
CONSTANT TaxRate <- 0.2
DECLARE Price : REAL
DECLARE Item : STRING
OUTPUT "Enter the item and its price"
INPUT Item
INPUT Price
OUTPUT Item, "costs", Price * (1 + TaxRate), "with tax"The data types
- INTEGER: a whole number, such as 42 or −7.
- REAL: a number with a fractional part, such as 3.75.
- CHAR: a single character, such as 'Y'.
- STRING: text of any length, such as "Redpen".
- BOOLEAN: TRUE or FALSE.
Input and output
INPUT reads a value into a variable, and OUTPUT shows values on screen. Several items can be output together by separating them with commas. A prompt before each INPUT tells the user what to type.
Where marks go
- Choosing INTEGER for something that can have a fractional part, like a price.
- Storing a phone number as INTEGER: it isn’t used in calculations and may start with 0, so STRING fits.
- Explaining a constant as “a value that doesn’t change” without saying why that helps.
- Writing an INPUT with no variable to store the value in.
- Mixing up CHAR (one character) with STRING.
Try a question
4 MARKS · MARKED ON THIS PAGEA stationery shop adds tax to every sale. Write pseudocode that: - declares a constant TaxRate with the value 0.2 - declares a REAL variable Price, an INTEGER variable Quantity and a REAL variable Total - inputs Price (the price of one item) and then Quantity (the number of items bought) - calculates Total as Price multiplied by Quantity, with tax added using TaxRate (Total = Price x Quantity x (1 + TaxRate)) - outputs "Total: " followed by Total rounded to 2 decimal places using ROUND Do not output any prompt messages.
solutionPseudocode
Loading...
Type your answer first.