Implementation: Computational constructsSelection - using if statements
Programs use computational constructs such as loops and predefined functions to break programs up into sections. This makes programs more compact, easier to write and easier to maintain.
Allowing programs to make decisions is an important part of programming. ‘If statements’ are used to let a program select an action depending upon the value of variables held in the program.
Here is an example of how an if statement can be used to select what happens next in a section of code designed to check the age of a driver.
IF age < 17 THEN
SEND “You are too young to drive” TO DISPLAY
ELSE
SEND “You are old enough to drive” TO DISPLAY
END IF
In this example, the program selects the text to be displayed on screen, depending upon the outcome of the IF statement.
The IF statement checks to see if the value of the age variable is less than 17. If this is the case, then the output is that you are too young to drive. If not, then you are told you are able to drive.