Basic Programming Syntax (Pseudocode)
Basic Programming Syntax (Pseudocode)
Contents
1 Comments 3
2 Data Types 4
3 Declarations 5
3.1 Variable Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3.2 Constant Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3.3 Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
4 Functions 7
4.1 Input and Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
4.2 Other Library Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
5 Operations 8
5.1 Arithmetic Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
5.2 Logical Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
5.3 String Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
7 Selections 12
7.1 IF statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
7.2 CASE statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
8 Arrays 14
8.1 1D Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
8.2 2D Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
9 File Handling 15
2
Comments
Comments in code act as a form of note that programmers might add to explain sections of programs
or other forms of logic. They are ignored by the compiler when the program is being executed. These
comments help make the code maintainable and manageable both for the program author and other
programmers.
Comments can be generalized into three forms, these are as follows:
• Multi-line comments
• Single-line comments
• Inline comments
In Pseudocode, all forms of comments are preceded with //.
Pseudocode
Multi-line comments
// This is an example of a multi-line comment
// It is normally used for larger explanations
Single-line comments
// This is an example of a single-line comment
Inline comments
OUTPUT Result // This is an example of an inline comment
3
Data Types
In programming, there are a few keywords used to designate certain data types, these are as follows:
INTEGER
REAL
CHAR
STRING
BOOLEAN
Literals
Literals of these data types are written as follows:
4
Declarations
In programming, a declaration is a statement that introduces an element to a program. It provides
the compiler with explicit information about the element before it is used. Additionally, by using
declarations, programmers enhance the accessibility and maintainability of their code.
In Pseudocode, there are two types of declarations, these are as follows:
• Variable declaration
• Constant declaration
Variable Declaration
Variables are declared in the following format:
DECLARE <identifier> : <data type>
Pseudocode
Constant Declaration
Constants are declared by stating the identifier and the literal value in the following format:
CONSTANT <identifier> ← <value>
Pseudocode
5
Assignment
In Pseudocode, the assignment operator is ←
Assignment statements are written in the following format:
<identifier> ← <value>
Pseudocode
6
Functions
In programming there are a few built-in library functions that may be provided.
Pseudocode
Input function
INPUT Answer
Output function
OUTPUT Score
Pseudocode
Rounding function
ROUND(3.1415, 1)
Returns 3.1
Random function
RANDOM() * 5
Returns a random decimal number between 0 and 5 inclusive
ROUND(RANDOM() * 10), 0)
Returns a random integer between 0 and 10 inclusive
7
Operations
In programming, there are three types of operations, these are as follows:
• Arithmetic operations
• Logical operations
• String operations
Arithmetic Operations
For arithmetic operations, standard mathematical operator symbols are used. These operator
symbols are as follows:
Pseudocode
Addition +
Subtraction -
Multiplication *
Division /
Raised to the power of ^
Pseudocode
Pseudocode
8
Logical Operations
The following symbols are used for logical operations:
Pseudocode
Equal to =
Less than <
Less than or equal to <=
Greater than >
Greater than or equal to >=
Not equal to <>
These operations evaluate expressions to BOOLEAN data type values.
String Operations
String operations are used to manipulate values of the STRING data type.
In Pseudocode, the basic string operations are written as follows:
LENGTH(<identifier>)
Returns the integer value representing the length of the string.
LCASE(<identifier>)
Returns the string/character with all characters in lower case.
UCASE(<identifier>)
Returns the string/character with all characters in upper case.
SUBSTRING(<identifier>, <start>, <length>)
Returns a string of the length length starting at the position start. The length and start
should be a positive integer.
Pseudocode
9
Iterations and Loops
In programming, there are three types of iterations. These are as follows:
• Count-controlled FOR loop
• Post-conditional REPEAT loop
• Pre-conditional WHILE loop
Pseudocode
Count-controlled loop
FOR Index ← 1 TO 30
OUTPUT Numbers[Index]
NEXT Index
10
Post-conditional REPEAT Loop
In post-conditional loop, the condition must be an expression that evaluates to an BOOLEAN data
type.
The statements in the post-conditional loop will be executed at least once. The condition is tested
after the statements are executed and if the condition evaluates to TRUE the loop is then terminated,
otherwise the process is repeated again.
In Pseudocode, post-conditional loops are written as follows:
REPEAT
<statements>
UNTIL <conditions>
Pseudocode
Post-conditional loop
REPEAT
INPUT InpPassword
UNTIL InpPassword = Password
Pseudocode
Pre-conditional loop
WHILE Number < 10 DO
OUTPUT Number
Number ← Number + 1
ENDWHILE
11
Selections
There are two functions of selections this booklet practices, these are as follows:
• IF statements
• CASE statements
IF statements
In Pseudocode, an IF statement without an ELSE clause is written as follows:
IF <conditions>
THEN
<statements>
ENDIF
IF statement with an ELSE clause is written as follows:
IF <conditions>
THEN
<statements>
ELSE
<statements>
ENDIF
Pseudocode
12
CASE statements
In Pseudocode, CASE statements allow one out of several branches of code to be executed
depending on the value of the variable.
CASE statements are written as follows:
CASE OF <identifier>
<value n> : <statements>
ENDCASE
An OTHERWISE clause can be added as follows:
CASE OF <identifiers>
<value n> : <statements>
OTHERWISE <statements>
ENDCASE
These clauses are tested in a sequence. When a value that applies is found, it’s statement is executed
and the CASE statement is completed. If present, an OTHERWISE clause must be the last case. It’s
statement will be executed if none of the preceding cases apply.
Pseudocode
CASE statement
CASE OF Choice
1 : OUTPUT "Birch wood selected"
2 : OUTPUT "Spruce wood selected"
3 : OUTPUT "Fir wood selected"
ENDCASE
13
Arrays
Arrays are fixed-length structures of elements that have identical data type, accessible by consecutive
index numbers. Square brackets, [<index>] are used to indicate the array indices.
In programming, arrays can have multiple dimensions, however this booklet only practices 1D and
2D arrays.
1D Arrays
In Pseudocode, 1D arrays are declared as follows:
DECLARE <identifier> : ARRAY[<l>:<u>] OF <data type>
Where l stands for lower bound and u stands for upper bound.
1D arrays are assigned in the following way:
<identifier>[<index>] ← <value>
Pseudocode
1D array declaration
DECLARE StudentName : ARRAY[1:30] OF STRING
1D array assignment
StudentName[19] ← "John Doe"
2D Arrays
In Pseudocode, 2D arrays are declared as follows:
DECLARE <identifier> : ARRAY[<lr>:<ur>, <lc>:<uc>] OF <data type>
Where r stands for row, c stands for column, l stands for lower bound and u stands for upper bound.
2D arrays are assigned as follows:
<identifier>[<ri>, <ci>] ← <value>
Where ri stands for row index and ci stands for column index.
Pseudocode
2D array declaration
DECLARE Grade : ARRAY[1:30, 1:5] OF CHAR
2D array assignment
Grade[16, 3] ← 'A'
14
File Handling
File Opening
When opening a file, the mode of operation of the file should be stated as follows:
OPENFILE <file identifier> FOR <file mode>
The file identifier will be the name of the file. The following file modes can be used:
READ for data to be read from the file
WRITE for data to be written to a file. In case the file does not exist, a new file will be created.
Existing data is overridden.
A file can only be opened in one mode at a time.
File Closing
When a file is no longer in use, it is closed as follows:
CLOSEFILE <file identifier>
Pseudocode
Writing to a file
WRITEFILE "Remarks.txt", Remark
Closing file
CLOSEFILE "Names.txt"
15
Procedures and Functions
Procedures and functions are always defined at the top of the program.
Procedures
In Pseudocode, a procedure with no parameters is defined as follows:
PROCEDURE <identifier>
<statements>
ENDPROCEDURE
A procedure with parameters is defined as follows:
PROCEDURE <identifier>(<par n> : <data type>)
<statements>
ENDPROCEDURE
When used, par n is the identifier for the parameters of the procedure. These will be used as
variables in the statements of the procedure.
Procedures should be called as follows:
CALL <identifier>
CALL <identifier>(<val n>)
When a procedure is called, if any parameters are present, they are substituted by the values, and the
statements are executed.
Pseudocode
16
Functions
In Pseudocode, functions behave the same way as procedures except it only returns one singular
value. During definition the data type of the value returned has to be stated.
A function with no parameter is defined as follows:
FUNCTION <identifier> RETURNS <data type>
<statements>
ENDFUNCTION
A function with parameter is defined as follows:
FUNCTION <identifier>(<par n>:<data type>) RETURNS <data type>
<statements>
ENDFUNCTION
Function calls are not a complete program statement; the keyword CALL should not be used when
calling a function. Instead, functions should only be called as part of an expression.
Pseudocode
Defining function
FUNCTION SumSquare(Num1:INTEGER, Num2:INTEGER) RETURNS INTEGER
RETURN Num1 ^ 2 + Num2 ^ 2
ENDFUNCTION
Using function
OUTPUT "Sum of squares: ", SumSquare(5, 10)
17