Sequence
sequenceIn computer programming, this is a set of instructions that follow on one from another. is the most common programming construct. In programming, statementThe smallest element of a programming language which expresses an action to be carried out. are executionThe process of a program being run on a computer. one after another. Sequence is the order in which the statements are executed.
The sequence of a program is extremely important. Carrying out instructionA single action that can be performed by a computer processor. in the wrong order leads to a program performing incorrectly.
This algorithmA sequence of logical instructions for carrying out a task. In computing, algorithms are needed to design computer programs. is designed to find the average of two whole numbers but the instructions are in the wrong sequence:
Here it is shown using pseudocode Also written as pseudo-code. A method of writing up a set of instructions for a computer program using plain English. This is a good way of planning a program before coding.:
num1 is integer
num2 is integer
average is real
average = (num1 + num2)/2
input 鈥淓nter first number鈥, num1
input 鈥淓nter second number鈥, num2
output 鈥淭he average is "&average
Running this program would result in an error because it tries to calculate the average before it knows the values of the numbers.
Here is the corrected algorithm:
This version has the instructions in the correct sequence:
num1 is integer
num2 is integer
average is real
input 鈥淓nter first number鈥, num1
input 鈥淓nter second number鈥, num2
average = (num1 + num2)/2
output 鈥淭he average is "&average
Having instructions in the wrong order is one of the simplest, yet most common, programming errors. It occurs no matter which programming language is used.