We are working with Cambridge Assessment International Education towards endorsement of this title. Any references or material related to answers, grades, papers or examinations are based on the opinion of the author(s). 2 Programming constructs
For example, when a satnav calculates an estimated arrival at destination time, the process follows a series of logical steps:
LE
Step 1 Obtain location of journey start point. Step 2 Obtain location of journey end point. Step 3 Calculate shortest route between the two points. Step 4 Calculate the time it will take to complete the journey. Step 5 Obtain the time the journey will start. Step 6 Calculate the estimated arrival time by adding the journey time to the start time. Obtaining the journey start and end points is crucial because without this information, the rest of the process could not be completed. However, the order in which the two locations is obtained is not that important. Had Step 1 and Step 2 been reversed, the calculation at Step 3 could still have taken place. In programming, the sequence is indicated by the order in which the code is written, usually top to bottom. The program will execute the first line of code before moving to the second and subsequent lines. An example of a sequence error would be completing a process before all the appropriate user inputs had been obtained. For example, trying to calculate the area of a rectangle when you only had the length of one side.
P
Selection
M
Often your programs will perform different processes dependent on user input. Consider a system designed to provide access to the school network when a user inputs a username and password. The system would need to follow different paths depending on whether the user input was accurate or not. One path would be followed if the username and password input matched records that would allow network access. If the username and password input did not match the records, the system would follow another path in which the network access would be denied and the user prompted to re-input the details.
Incorrect
User Name inputted and checked against password.
Correct
Access granted.
A
Access denied and user prompted for new password.
Figure 2.1: Different paths needed on input of username to access school network
S
Iteration
It is common for a program to perform identical processes on different data items. Consider a program that produces a line graph. It takes a series of coordinates and draws a line. The code that provides the instructions that plot the new coordinates and draws a connecting line from the previous coordinates will be repeated for each coordinate given. Iteration allows the execution of the code to jump back to the beginning of the code sequence that provides instruction on how to ‘plot’ every time a new set of coordinates is entered.
23 Original material © Cambridge University Press 2021. This material is not final and is subject to further changes prior to publication.