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.
For example, if i were to hold the value 4, then name[i] would represent 'molly'
The command can be followed by text in quotes, which is output verbatim, and variables, whose value is output. Some examples:
print 'Please enter an integer'
print 'The current counter value is ', count
For example,
n = input('Enter your age in years')
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: <, <=, >, >=, = and logical operators, AND, OR, NOT.
where statements... indicates any statements may be placed indented after the if statement. 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):
statements...
else:
statements...
while (condition):
statements...
tests the condition. If it is true, the indented statements are executed, and then the condition is tested again. When the condition is false, control falls out of the loop.
n = input('Please enter a positive integer value')
count = 1
while (count <= n):
print count
count = (count + 1)
Alternatively one can iterate a specific number of times using the for construct:
for i in range (x):
This iteration construct will repeat the given indented code x times. Furthermore, each time through the iterated code the variable i will be incremented by 1 (starting off with the value of 1).
n = input('Please enter a positive integer value')
for count in range(n):
print count