CSCI 170 PseudoCode Conventions

Introduction

An algorithm is a well-ordered collection of unambiguous, effectively computable instructions that produce a result and halt in a finite amount of time.

The instructions that are unambiguous and effectively computable depend on the computing agent executing the algorithm. Establishing a well-ordered collection of those instructions depends on the language used to describe the algorithm. While we can write algorithms for ourselves without deep reflection on this definition (since we understand what is effectively computable and unambiguous for ourselves), if we want to communicate algorithmic solutions to others, we must establish a reasonable common computing agent. Below we describe a set of instructions that will define a language and effectively computable instructions for a pseudomachine that will serve as our target computing agent.

Computational State

Our computing agent must be able to keep track of a variety of values needed in executing the algorithm. The values involved in the execution of an algorithm make up the state of the computation. The state is dynamic over the execution time of the algorithm, as values associated with particular attributes of the algorithm may change as the algorithm executes.

As algorithm writers, we need a way to describe the various values in the state of the computation. Each value will be describe with a name, written in lowercase letters, and called a variable. For example, we may want to write an algorithm that outputs the integers between one and ten. In order to know which value to output next, we could establish a variable count that will represent the count to output next.

As an algorithm designer you should think of a variable as a box holding a value. The name of the variable (e.g. count) is the unique name of the box.

Lists of values and indices

Sometimes we need to keep track of lists of associated values, for example a list of ten names. Rather than think up ten different variable names, we can use list notation and refer to name[1], name[2], ..., name[10] to refer to the ten names. We say that name is the list of values and that the integer in [] is the index indicating which value in the list we are referring to.

When using a list of values, we may use a variable to keep track of which item in the list we are interested in. For example we may have a variable i that keeps track of which name we want to look at. name[i] would then refer to the value in list name found at the index indicated by the value of i.

Interacting with the User

After an algorithm designer writes an algorithm, a user decides to run the algorithm on a particular computing agent. The computing agent will need to interact with the user at least once, when outputting the result to the user. The agent may also need to receive information from the user.

Output from computing agent to user

Any of the following may be used to indicate the agent is outputting information to the user: Output
Print
Display

The command can be followed by text in quotes, which is output verbatim, and variables, whose value is output. Some examples:

Output "Please enter an integer"
Output "The current counter value is " count

Input from user to agent

Input from the user is indicated by Input followed by a list of variables that will store the values input from the user.

For example,
Input n
inputs a single value, storing it into the variable named n.

Changing the value of a variable

Algorithms need to be able to change the value of variables. To set the value of a variable to a new value we will use the following command:
Set variable To value
Where variable is the name of the variable whose value the algorithm is changing and value is an expression whose value should be stored in the variable.

What are expressions?

Many times, the expressions set into the variable are mathematical expressions. The usual operators +, -, *, / are available. We also have two other operators, div and mod. The value x div y is the integer portion of x / y. The value x mod y is the integer remainder of x / y. For example, 7 div 2 is 3 and 7 mod 2 is 1 (because 2 goes into 7 three times with a remainder of 1).

Expressions can also be boolean. Boolean expressions have only two possible values, true or false. The operators used in boolean expressions include the usual comparison operators: <, &le, >, &ge, = and logical operators, AND, OR, NOT.

Conditional statements

To describe a point in the algorithm in which steps are executed only if some condition is true, we use the if statement.
if (condition) then
BEGIN
    statements...
END

statements... indicates any statements may be placed between the BEGIN/END. The statements are executed only if the condition expression evaluates to true.

One may also want to indicate an alternative set of statements to be executed if the condition evaluates to false. In this case the statement is written as:
If (condition) Then
BEGIN
    statements...
END
Else
BEGIN
    statements...
END

Iteration

To indicate repetition of statements in an unambiguous way, the algorithm will use one of the following statements:

Repeat
    statements...
Until (condition)
executes the statements inside the Repeat/Until block, then tests the condition. If the condition is false, the statements are executed again. Note that the statements are always executed at least once.

RepeatWhile (condition)
BEGIN
    statements...
END
tests the condition. If it is true, the statements inside the BEGIN/END block are executed, and then the condition is tested again. When the condition is false, control falls out of the loop.

Example

The following algorithm inputs an integer and outputs the values between 1 and that value.

Output "Please enter a positive integer value"
Input n
Set count To 1
RepeatWhile (count < n+1)
BEGIN
    Output count
    Set count To (count + 1)
END

Note: With careful use of indentation, the BEGIN/END pair may be omitted since they are understood.