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.
subroutineA section of code written outside of the main program. The umbrella term for procedures and functions. are small blocks of code in a modular program designed to perform a particular task. Since a subroutine is in itself a small program, it can contain any of the sequenceIn computer programming, this is a set of instructions that follow on one from another., selectionA decision within a computer program when the program decides to move on based on the results of an event. and iterationThe repetition of a block of statements within a computer program. constructs.
In the following example, the algorithmA sequence of logical instructions for carrying out a task. In computing, algorithms are needed to design computer programs. asks the user to enter two numbers. It then adds them together and, if the total is over 10, it runs the ‘CountDown’ subroutine. If the total is not over 10, it will run the ‘CountUp’ subroutine.
Here is the same algorithm written 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.:
declare CountDown while total <> 0 output total total = total – 1 repeat end Subroutine declare CountUp i is integer set i = 0 while i <> total output i i = i + 1 repeat end Subroutine num1 is integer num2 is integer total is integer input “Enter first number”, num1 input “Enter second number”, num2 total = num1 + num2 if total > 10 call CountDown else call CountUp end if