Programming
Concepts
Declaring
Variables and constants can be declared with a certain data type before they are assigned a value. This makes it easier to understand the purpose of a program and identify errors.
DECLARE Text : STRING CONSTANT DaysPerWeek ← 7
Data Types
There are 5 basic data types when using pseudocode.
- boolean
- integer
- real
- char
- string
Common Techniques
Selection
Some section of a program may require a decision to be made. These decision are often controlled by booleans. Selection allows parts of a program to be ignored depending on the value of a variable or boolean expression.
Most selection statements are done using an IF
statement which runs a block of code if the condition is true.
Num ← 1 IF Num > 0 THEN OUTPUT "Positive" ELSE OUTPUT "Not Positive" ENDIF
On the other hand, a CASE
statement runs a block of code when a variable is equal to value. This statement is used when it would take too many IF
and ELSE
statements to account for multiple cases.
X ← 0 Y ← 0 OUTPUT "Make your move!" INPUT Move CASE OF Move "w" : Y ← Y + 1 "s" : Y ← Y - 1 "d" : X ← X + 1 "a" : X ← X - 1 OTHERWISE OUTPUT "?" ENDCASE OUTPUT X OUTPUT Y
Iteration
Repeating a process is called iteration. In programming, loops are used to perform tasks repeatedly. There are 3 types of loops.
- count-controlled
FOR
loops - pre-condition
WHILE
loops - post-condition
REPEAT UNTIL
loops
String Handling
Strings are an important aspect of programming. They are similar to an array of characters (char array).
- length
- substring
- upper
- lower
OUTPUT LENGTH("penis") OUTPUT UCASE("#2c6fef") OUTPUT LCASE("X") OUTPUT SUBSTRING("sushi", 1, 3)
Operations
There are 3 types of operations:
- Arithmetic
- + (addition)
- - (subtraction)
- * (multiplication)
- / (division)
- ^ (exponentiation)
- Logical
- = (equal to)
- <> (not equal to)
- > (greater than)
- < (less than)
- >= (greater than or equal to)
- <= (less than or equal to)
- Boolean
- AND
- OR
- NOT
Procedures and Functions
A procedure is a block of reusable code that performs a task.
DECLARE Items : ARRAY PROCEDURE Swap Temp ← Items[Index] Items[Index] ← Items[Index + 1] Items[Index + 1] ← Temp Swapped ← TRUE ENDPROCEDURE N ← 0 Item ← "" REPEAT OUTPUT "Enter an item or leave blank to begin sorting." INPUT Item IF Item <> "" THEN N ← N + 1 Items[N] ← Item ENDIF Length ← N UNTIL Item = "" Swapped ← TRUE WHILE N > 1 AND Swapped DO Swapped ← FALSE N ← N - 1 FOR Index ← 1 TO N IF Items[Index] > Items[Index + 1] THEN CALL Swap ENDIF NEXT ENDWHILE OUTPUT Length, " items have been sorted!" FOR Index ← 1 TO Length OUTPUT Items[Index] NEXT
A function is similar to a procedure but it returns a value back to the main program. This makes functions very handy for calculations or recursion.
FUNCTION SumUpTo(Value) RETURN Value / 2 * (Value + 1) ENDFUNCTION OUTPUT SumUpTo(4)
Library Routines
Library routines are pre-written functions in pseudocode that are used to perform common tasks. Programming languages tend to collectively call this a standard library.
MOD
- returns the remainder after a division operation
DIV
- returns the floor of a division operation (performs integer division and rounds the result down)
ROUND
- rounds a number to the nearest integer
RANDOM
- returns a random decimal number between 0 and 1
Maintainable Program Standards
Good programmers create maintainable programs. A maintanable program includes the appropriate use of:
- meaningful identifiers (to allow programmers to easily understand the purpose of a variable)
- the commenting feature provided by the programming language (to help programmers know the purpose of a section of code)
- procedures and functions (to make programs modular and easy to update)
- relevant appropriate commenting of syntax
A meaningful identifier exlpains the purpose of a variable. For example, a variable used to store a running total should have a name like Total
or Sum
.
Arrays
Arrays are fixed-length structures containing elements with the same data type which are accessible by consecutive index numbers. It is good practice to explicitly state what the lower bound of the array (the index of the first element) because this defaults to either 0 or 1 in different systems. Generally, a lower bound of 1 will be used in pseudocode.
DECLARE TicTacToe : ARRAY[1:3, 1:3] OF CHAR
File Handling
Storing data in files permanently allows data to be resued by a program even after it has been closed and reopened. Files also allow data to be transferred between machines.
The pseudocode below shows the process of writing the text stored in FileA.txt
to FileB.txt
. This will replace the existing contents of FileB.txt
but leave FileA.txt
unmodified.
DECLARE LineOfText : STRING OPENFILE FileA.txt FOR READ OPENFILE FileB.txt FOR WRITE READFILE FileA.txt, LineOfText WRITEFILE FileB.txt, LineOfText CLOSEFILE FileA.txt CLOSEFILE FileB.txt
Deez Nuts!