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.
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.
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.
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
For example,
Input n
inputs a single value, storing it into the variable named n.
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.
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
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.
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.