Programming and Data Representation
Writing Paper 2 pseudocode: constants, variables and operators, the insert's functions, IF, CASE and the three loops, and procedures and functions with their parameters.
Constants, variables and assignment
Declare each variable with its type, store values with the arrow, and give fixed values a constant name.
A variable is a named store whose value can change. Declare every variable with its data type:
DECLARE Mark : INTEGER
Assignment uses the arrow. Mark ← 0 stores the value on the right in the variable on the left.
A constant is declared once, with a literal value: CONSTANT PassMark = 50. The program cannot change it.
Watch out:A constant's value must be a literal, never a variable or an expression.
Why use a constant? The code is easier to read. And if the value changes, you edit one line.
| Statement | What it does |
|---|---|
INPUT | reads a value from the keyboard into a variable |
OUTPUT | writes values to the screen, separated by commas |
Algorithm Design shows how a flowchart or structured English becomes these statements.
CONSTANT PassMark = 50 DECLARE Name : STRING DECLARE Mark : INTEGER OUTPUT "Enter your name" INPUT Name OUTPUT "Enter your mark" INPUT Mark IF Mark >= PassMark THEN OUTPUT Name, " passed" ELSE OUTPUT Name, " needs ", PassMark - Mark, " more marks" ENDIF
Operators and expressions
Operators do maths, compare values, join conditions and join strings; brackets control the order.
| Kind | Operators | Example |
|---|---|---|
| arithmetic | +, -, *, /, DIV, MOD | see below |
| comparison | >, <, >=, <=, =, <> | always give a BOOLEAN: TRUE or FALSE |
| logic | AND, OR, NOT | Mark >= 50 AND Mark <= 79 |
| string | & | joins two strings |
Three ways to divide:
/always gives a REAL:7 / 2is 3.5.- DIV gives the whole-number part of a division:
7 DIV 2is 3. - MOD gives the remainder:
7 MOD 2is 1.
Multiplication and division are done before addition and subtraction.
Remember:Brackets make the order clear, so use them in long expressions.
OUTPUT 2 + 3 * 4 // 14 OUTPUT (2 + 3) * 4 // 20 OUTPUT 7 / 2 // 3.5 OUTPUT 7 DIV 2 // 3 OUTPUT 7 MOD 2 // 1 OUTPUT 7 > 2 // TRUE OUTPUT "Summer" & " " & "Pudding"
Built-in functions: use the insert
The exam insert lists the built-in functions; learn to use them, not to remember them.
Positions in a string start at 1.
| Function | Gives |
|---|---|
LEFT(s, x) | the leftmost x characters |
RIGHT(s, x) | the rightmost x characters |
MID(s, x, y) | y characters, starting from position x |
TO_UPPER, TO_LOWER | the CHAR or STRING in a different case |
NUM_TO_STR, STR_TO_NUM | a number as text, or text as a number |
IS_NUM | TRUE if a string is a number |
ASC, CHR | a character's ASCII code, or the character for a code |
INT(x) | the integer part of a real |
RAND(x) | a random real from 0 up to, but not including, x |
Watch out:Asking for characters past the end of the string is an error.
// each comment is the value returned DECLARE City : STRING City ← "Ulaanbaatar" OUTPUT LEFT(City, 5) // "Ulaan" OUTPUT RIGHT(City, 6) // "baatar" OUTPUT MID(City, 6, 6) // "baatar" OUTPUT LENGTH(City) // 11 OUTPUT TO_UPPER("hi") // "HI" OUTPUT TO_LOWER("HI") // "hi" OUTPUT NUM_TO_STR(87.5) // "87.5" OUTPUT ASC('A') // 65 OUTPUT INT(27.5415) // 27
IF, ELSE and nested IF
IF runs code only when a condition is TRUE; a nested IF asks a second question inside the first.
IF … THEN … ENDIFruns its statements only when the condition is TRUE.- An
ELSEpart holds the statements to run when it is FALSE.
An IF can sit inside the THEN part or the ELSE part of another IF. This is a nested IF.
When to use one: when a second question only makes sense after the answer to the first.
Example:Only a mark of 50 or more can be a Distinction. So the program asks "is it 80 or more?" only after "is it 50 or more?".
Two rules:
- Every IF needs its own ENDIF.
- Indent the statements inside each part. Then a reader can see which ELSE belongs to which IF.
DECLARE Mark : INTEGER INPUT Mark IF Mark >= 50 THEN IF Mark >= 80 THEN OUTPUT "Distinction" ELSE OUTPUT "Pass" ENDIF ELSE OUTPUT "Fail" ENDIF
CASE: one variable, many values
CASE compares one variable with a list of values, and only the first match runs.
CASE OF compares one variable with a list of values or ranges, such as 80 TO 100.
How it works:
- The clauses are tested in order.
- Only the first clause that matches runs.
- The clauses after it are not tested.
OTHERWISE catches every value not listed. It must be the last clause.
Watch out:Overlapping ranges are a bug. Only the first match runs, so the later clause never gets the values they share.
When to use CASE: instead of a chain of IFs, when every test compares the same variable with fixed values.
DECLARE Mark : INTEGER INPUT Mark CASE OF Mark 80 TO 100 : OUTPUT "Distinction" 50 TO 79 : OUTPUT "Pass" 0 TO 49 : OUTPUT "Fail" OTHERWISE : OUTPUT "Not a valid mark" ENDCASE
The three loops
FOR repeats a fixed number of times, WHILE tests first, and REPEAT tests last.
| Count-controlled loop | Pre-condition loop | Post-condition loop | |
|---|---|---|---|
| Written | FOR … TO … NEXT | WHILE … ENDWHILE | REPEAT … UNTIL |
| Tests | no test: a fixed number of times | the condition first | the condition last |
| Fewest runs | zero, if the start is already past the end | zero | always at least once |
| Stops when | the counter passes the end | the condition is FALSE | the condition becomes TRUE |
In a FOR loop the counter goes up by 1 each time. STEP sets a different change, and it can be negative.
Watch out:WHILE stops when its condition is FALSE. REPEAT stops when its condition becomes TRUE.
DECLARE Count : INTEGER // STEP -3 counts down in threes FOR Count ← 10 TO 1 STEP -3 OUTPUT Count NEXT Count
Choosing a loop, and saying why
Choose the loop by what you know before it starts, and give a reason that fits the task.
| Use | When | Example |
|---|---|---|
| FOR | the number of repetitions is known | once for each of the 30 elements of an array |
| REPEAT | the body must run at least once | asking for a value again until it is valid |
| WHILE | the body may not need to run at all | reading a file until EOF: the file may be empty |
Exam:A mark scheme usually gives one mark for the loop, and one for a reason that fits the task.
Procedures: a named block you CALL
A procedure is a named block of code for one job; you define it once and run it with CALL.
A procedure is defined once, with PROCEDURE … ENDPROCEDURE. It is run with CALL, on a line of its own. It may have no parameters, one, or more.
When to use one:
- the same task is needed in several places
- to break a program into modules
When the procedure ends, the program carries on from the line after the CALL.
| Declared | Kind | Who can use it |
|---|---|---|
| inside a procedure | local variable | only that procedure; it uses memory only while the procedure runs |
| in the main program | global variable | every part of the program |
PROCEDURE ShowStars(Number : INTEGER) DECLARE Stars : STRING DECLARE Count : INTEGER Stars ← "" FOR Count ← 1 TO Number Stars ← Stars & "*" NEXT Count OUTPUT Stars ENDPROCEDURE CALL ShowStars(3) CALL ShowStars(5) OUTPUT "Done"
Functions: a call that becomes a value
A function works like a procedure but hands back one value, so you use it inside an expression.
A function is like a procedure, but it returns one value.
- Its header says the type of that value, for example
RETURNS INTEGER. RETURNsends the value back and ends the function at once.
How to call it: inside an expression, never with CALL. The value it returns replaces the call. Then the expression is worked out.
Example:In Larger(Test1, Test2) + 5, the call is replaced by the larger mark. Then 5 is added.
Remember:Use a function when the job of a module is to work out a single value.
FUNCTION Larger(A : INTEGER, B : INTEGER) RETURNS INTEGER IF A > B THEN RETURN A ELSE RETURN B ENDIF ENDFUNCTION DECLARE Test1 : INTEGER DECLARE Test2 : INTEGER INPUT Test1 INPUT Test2 OUTPUT "Best mark: ", Larger(Test1, Test2) OUTPUT "Bonus mark: ", Larger(Test1, Test2) + 5
By value or by reference
BYVAL hands over a copy; BYREF hands over the caller's own variable, so changes come back.
| BYVAL | BYREF | |
|---|---|---|
| The parameter gets | a copy of the argument | the caller's own variable |
| A change inside the procedure | does not change the caller's variable | does change it |
| Default | yes: if no keyword is written, BYVAL is assumed | no |
One keyword covers the parameters after it. So PROCEDURE SWAP(BYREF X : INTEGER, Y : INTEGER) passes both X and Y by reference.
Use BYREF only when a procedure must hand values back.
Watch out:Never use BYREF with a function. The guide says parameters should not be passed by reference to a function.
// one BYREF covers X and Y PROCEDURE SWAP(BYREF X : INTEGER, Y : INTEGER) DECLARE Temp : INTEGER Temp ← X X ← Y Y ← Temp ENDPROCEDURE DECLARE A : INTEGER DECLARE B : INTEGER A ← 1 B ← 2 CALL SWAP(A, B) OUTPUT "A = ", A, ", B = ", B
The words: header, interface, parameter, argument, return value
Five words describe a subroutine: header, interface, parameter, argument and return value.
| Word | Meaning |
|---|---|
| Header | the first line of a procedure or function: its name, its parameters with their data types and, for a function, the type it returns |
| Interface | what a caller needs to know to use it: the name, the number, order and types of the parameters, and the return type |
| Parameter | a name in the header that receives a value |
| Argument | the actual value or variable passed in the call |
| Return value | what a function sends back with RETURN; it replaces the call |
Example:In the box below, A and B are parameters. Test1 and Test2 are arguments.
FUNCTION Larger(A : INTEGER, B : INTEGER) RETURNS INTEGER OUTPUT "Best mark: ", Larger(Test1, Test2)
Writing efficient, maintainable pseudocode
Efficient code does no unnecessary work; maintainable code is easy for someone else to read and change.
Efficient code does no unnecessary work:
- Stop a loop as soon as its job is done. A search should use a conditional loop, not a FOR loop that always visits every element.
- Do not repeat a calculation inside a loop when its value does not change. Work it out once, before the loop.
- Do not copy code that a procedure could hold.
Maintainable code can be read and changed by someone else. Use:
- meaningful identifiers
- constants instead of unexplained numbers
- comments that start with
// - indentation
- modules
RUN THE PSEUDOCODE
Write the pseudocode your syllabus uses and press Run. Assignment is the arrow — type <- and it works the same as ←. Both syllabuses are accepted, so OUTPUT and PRINT both work; where a word differs from the one the exam prints, the runner says so.
Press Run to see what it doesTotal ← 0
FOR Count ← 1 TO N
Total ← Total + Count
NEXT Count
OUTPUT TotalSTARTN = 5
| Count | Total | OUTPUT |
|---|---|---|
One row per pass through the loop. Leave a cell blank if it does not change on that pass.
KEY TERMS
CHECK YOURSELF
1.A procedure changes a parameter passed BYVAL. What happens to the caller's variable?
2.Which should you use when the code must give a value back for use in an expression?
3.What does 7 / 2 give in 9618 pseudocode?
4.A CASE lists 50 TO 100 : OUTPUT "Pass" before 80 TO 100 : OUTPUT "Distinction". What is output for a mark of 85?
5.Which loop always runs its body at least once?