Pseudocode Language Specification

version 0.7.0
Quick Access: Features and Rules Keyword Glossary Pseudocode Files

Purpose

I believe that doing pseudocode before you code points out many problems and allows clear thinking. I also think that pseudocode is the quickest way to communicate your ideas across to other developers. To speed things along I have created this document to give the information needed for other developers to read and write in pseudocode. I have based it upon what I have learnt at university, and made further refinements to create an object-oriented pseudocode language that is independent of any one language. I am happy to take suggestions and make improvements.

Back to the top of the page

Features and Rules

Below is a short line by line listing of the features and rules of the pseudocode language. This is not quite an exhaustive list but if you have already programmed before then you'll be able to pick up the subtle aspects from the example files, and as I have said before, any questions just ask me.

Back to the top of the page

Keyword Glossary

If you are just starting out I recommend that you read it from the top to the bottom as I have tried to arrange it so that each section will build on previous knowledge. After you have read the specification, then you'll be able to understand the example file much better. If you have any questions feel free to ask me about them. Below is a quick reference to the various sections addressed in the pseudocode specification. If you already familiar with it, then it'll be of great use as a reference.

Sections: Comments Maths Operators Literals Statement Blocks Grouping Variables Input & Output Conditional Construction Loop Construction Arrays Functions Classes & ADTs Error Handling Back to the top of the page

Comments:

As with any good code it should be commented well. Comments are of course not executed like normal languages. Multiline comments are surrounded by braces {}. Single line comments use the hash mark #. Comments should be written assuming that they will carry over into the code. For an example:
{ I am a
multiline comment.}
# I am a single line comment.

Back to Keyword Listing

Maths operators:

The following are known math operators: addition (+), subtraction (-), multiplication (*), division (/), modulus (%), the exponent (^), less than (<), equal (=), greater than (>), less than or equal to (<=), and greater than or equal to (>=).

2 + 2 Result: 4
2 - 2 Result: 0
2 * 2 Result: 4
2 / 2 Result: 1
2 % 2 Result: 0
2 ^ 2 Result: 4
2 < 2 Result: False
2 = 2 Result: True
2 > 2 Result: False
2 <= 2 Result: True
2 >= 2 Result: True

Back to Keyword Listing

Assignment operator:

variable := expression
The assignment operator looks like the following :=. Note that it is not the same as =, which is a test for equivalence. An assignment operator will take the value from the right side and replace the value of the left side with it. For an example:
n := 5;
Will give n the value of 5.
Back to Keyword Listing

Literals

There are two type of literals: a string literal and a character literal. A character literal is used by placing single quotes around the character. A string literal is used by placing double quotes around the text. For an example:
character c := 'a'
string s := "This is a string."

Back to Keyword Listing

Statement Blocks:

Statements are all blocked off with the keywords begin and end. Each statement in the block is executed sequentially. Statements inside these blocks should carry the same indentation to ease reading. For an example:
begin
statement1;
statement2;
statement3;
...
statmentN;
end

Back to Keyword Listing

Grouping:

You can group expressions using parentheses (). As in maths, parentheses have the highest order of precedence. Another form of group can be achieved using the comma (,). This will extend a statement by one. For an example:
b := (a / 2) + 6, a := 1

Back to Keyword Listing

Variables:

Variables must always be declared before they are used. Variables are only known within their block statement scope. To create a variable use the keyword declare before the variable type. After the variable type follow with the variables name. A variable can be made constant by using the keyword constant. Constants must be assigned a value at the time of their declaration. A global variable can be made to exist only within the scope of the file by using the keyword permanent. This has the same usage and meaning as found in C++. You can create a user defined type by using the keyword enumeration followed by the type name. After the type name give an integer list of values. All enumerations start at zero and follow sequentially, unless otherwise specified using an assignment operator. For an example:
declare integer i;
declare constant integer j := 12;
declare permanent integer j;
enumeration Days begin Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday end;

Back to Keyword Listing

Input/Output

Input from a user is easily done using the keyword read then followed by the variable you want the input to be filled with. This keyword indicates that you will read the input for the user from the standard input. Output for a user to read from the standard output is indicated by the keyword print or printLine followed by what you want to have displayed to the user. The only difference between print and printLine is explained below. If there is an error that you would like to have printed to the standard error use the keyword printError. For the keywords printLine and printError adds a newline at the end of the statement. For an example:
string message;
read message;
print "You wrote " + message;
printError "Something horrible has happened.";

Back to Keyword Listing

Conditional Construction:

The basic if-construct support is: if condition then statement else statement. You can extend the if statement by using the elif keyword, which acts as an else-if construct statement. For example:
if a = 1 then
print "a is 1.";
elif a = 2 then
print "a is 1.";
else
print "a is not 1 or 2.";

If you need to test just one variable for several conditions, then a case statement would be more appropriate. There are three keywords involved in a case statement: case, select, and default. The keyword case is followed by the variable you want to test. Blocking that off you precede each test using the keyword select. To handle a default case selection use the keyword default. Note that unlike some languages, we do not have a fall through property. A specific series of select statements are executed until the next select statement is declared. For an example,
case a
begin
select 1
print "a is 1.";
select 2
print "a is 2.";
default
print "a is not 1 or 2";
end

Back to Keyword Listing

Loop Constructions:

for looping_variable := initial value to final value stepping value
statement;
The purpose of a loop is to execute the same sequence of statements given certain conditions. If you want to completely exit prematurely from any loop construct use the keyword break. Break will exit out of the first loop construct that it encounters. If you want to go on to the next iteration in the loop construct use the keyword continue. Continue will stop the loop execution and restart the loop construct at the next iteration. The for loop is fairly straight forward as its use is based on numerical iterations. There is a looping variable with an inclusive starting point and an ending point that is exclusive. The for loop will execute a single statement, which follows the final value on a new line. The for loop can of course be made to execute more statements by using block statements. The looping variable follows the keyword for. After the looping variable is the assignment operator giving its initial value. The final value follows the keyword to which follows the initial value. During execution, the for loop will test to be sure the looping variable is within the specified range, and if so will execute the statement. After executing the statement, the for loop will increment the looping variable by one (the default) and re-test to see if the looping variable is still within range. If you do not want to increment by one you can use the stepping keyword to change how the for loop increments. For an example:
for integer j := 2 to 14 stepping 2
i := i * j;

while condition
statement;
The keyword while precedes any conditional expression. The statement that the while loop should execute follows on a new line. The while loop can be made to execute more lines of code by using block statements. The while loop only executes only if the condition supplied evaluates to true. After executing the statement, it retests the condition. For an example:
declare integer k := 12;
while k < 24
k := k + 1;

Back to Keyword Listing

Arrays:

To declare that a variable is an array structure use the keyword array after the type of the variable. After the declaration you can access any element in the array structure using square brackets to index an array. As any good hacker will tell you, arrays always start with 0, and so will ours. You can specify how many elements the array should initially have by wrapping the number desired in parentheses following the variable name. As far as type safety and method calls you can treat the array storage in the pseudocode similar to the arrays in Java™ or like a C-style array depending on the target language. For an example:
integer array myArray(6);
for integer i := 0 to myArray.size()
myArray[i] := 0;

Back to Keyword Listing

Functions:

virtual abstract procedure return_type proc_Name(variable1_type variable1: this is the first variable, ...)
begin
statement1;
statement2;
statement3;
...
statmentN;
end
A function is a statement that can be called to execute a specific sequence of statements. To call a function write the function's name followed by parentheses, passing any necessary arguments inside the parentheses. If the function being called takes more than one argument separate each argument by a comma.
Some function you write may need a return type. Return types are declared by listing the return type following the keyword procedure. If your function does not have a return value, then use the keyword noGive. To return a specific value or expression use the keyword give inside the function, placing give before the value or expression you wish to return. The statements for the function will follow on a new line.
The statements of a function must be enclosed by block statements. An argument list can be provided after the function's name inside of the parentheses. The description will follow after the variable name preceded by a colon. This should aid in the documentation later on. The arguments type will preceded the arguments name. Each variable in the procedure list is separated by a comma.
A function has scope. It can be either global or only within a class. A global function is created by not having the function inside a block statement. When a procedure is inside of a class it may have additional properties listed before the procedure keyword. These keywords are virtual, thread and abstract. I am sure you are familiar with virtual and abstract and will not waste time explaining them. The thread keyword is used to indicate that this function is to be threaded in the implemented language. The actually starting of a threaded function is done with the keyword launch.
There are three kinds of special function constructor, destructor, and startHere. The keyword procedure is not needed in these specific cases. Constructors and destructors both hold the same meaning as they would in C++. The startHere special procedure is where execution would begin in a programme. An example for a general function is:
procedure real randomMultiply(real x: is the multiplicand)
begin
n := randomNumber();
give n * x;
end

Back to Keyword Listing

Classes and Aggregate Data Types:

All classes can be created using the keyword class. A class can be made up of other known classes using the keyword inherits after the name of the subclass. Procedures listed in the class can be given three kinds of scope public, protected, and private. These scopes carry the same meaning as in C++. If no scope is listed anywhere in the class everything is assumed to be public, else the last declared scope is used. Scopes do not need blocks, but they do need to be on their own line.
The keyword attribute is used to declare class member variables. When declaring a class member variable the keyword declare is not necessary as it is implied with attribute. As always you can use blocks to extend the statements. If a constructor and/or a destructor is not explicitly declared they will be created. For how to signify a constructor and a destructor see the information given in the procedures section.
If a file is going to be needed when the pseudocode is converted and compiled into the specified language, then the keyword addin is used before the name of the file to indicate the inclusion of that file into the code.
When you need to call the method of a base class use the keyword base before the method's name. The only exception is when you are calling the base class's constructor, in that case you only need to use the keyword base.
If you need to specify the scope of something you may use the keyword in, proceeded by the name of the variable you're intrested in and followed by the location of that variable. A class must be enclosed with a unique name using the keywords scope and scopeEnd. The keyword scope is similar to the #include and the #ifndef in C++ combined. The keyword scopeEnd is similar to #endif in C++. An example is given below:
scope FOO_H_
addin bar.h

class Foo inherits Bar
begin
public
permanent integer m := 6;

constructor()
begin
n := 5;
base("15");
end

procedure noGive display()
begin
print n;
end

private
attribute integer n;
end;
scopeEnd

An aggregate data type is very similar to a class with the only difference being that a class can have different kinds of scope and a class has inheritance. The keyword for creating an aggregate data type is structure and then the data type name. Just like a class the type name of a structure should be capitalised. For an example:
structure Card
begin
string face;
string suit;
end;

Back to Keyword Listing

Error Handling

Error handling is not something that is exactly specified as it takes on the form of the implemented language. The three keywords are: attempt, error, handle. The keyword attempt is used like in C++'s try keyword and also when the pseudocode is being written for a programme that will be done in BASIC or some similar language it carries the meaning of the following phrase ON ERROR GOTO. The keyword error has a dual meaning. If the programme is to be in C++ then error is like the keyword throw. If the programme is to be in BASIC then error is like the keyword ERR. The keyword handle carries the same meaning in C++ as the keyword catch and the same meaning in BASIC as the keyword RESUME. Here are some examples:
If a programme was to be implemented in C++:
attempt
begin
declare integer i := 0;
declare integer j := 2;
declare integer result;

if i = 0 then
error "Divide by zero.";
else
result := j / i;
end

handle string message
begin
printError message;
end

If a programme was to be implemented in BASIC here is an example:
attempt Handler;
declare integer x := 5/0;

... bunch of BASIC statements ...

Handler:
if error = 23 then
begin
print "Can't divide by 0";
x := 0;
handle NEXT;
end

Back to Keyword Listing

Pseudocode Files:

To use the files just simply download them to your computer and/or open them up in your favourite text editor.

Example File

Kate/Kwrite syntax highlighting file

Back to the top of the page
Valid HTML 4.01! Valid CSS!

© 2005, 2006 by John Schneiderman, FDL Licence Information
Back to gpc homepage
Last Updated on 10 Oct 06