Computational constructsSelection - Using If statements
Programs run in sequences. Selection statements and loops are used in programs, allowing software to make decisions and repeat actions. These are called computational constructs
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 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.