大象传媒

Programming constructs - EduqasSequence

Programs are created using common building blocks, known as programming constructs. These programming constructs form the basis for all programs and are also used in algorithms.

Part of Computer ScienceSystems analysis

Sequence

is the most common programming construct. In programming, are one after another. Sequence is the order in which the statements are executed.

The sequence of a program is extremely important. Carrying out in the wrong order leads to a program performing incorrectly.

An explanation of sequencing, as used in algorithms and programming

This is designed to find the average of two whole numbers but the instructions are in the wrong sequence:

An incorrect flowchart for an algorithm designed to find the average of two whole numbers

Here it is shown using :

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:

An correct flowchart for an algorithm designed to find the average of two whole numbers

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.