EvaluationUsing selection statements

Appropriate use of coding constructs is evaluated to determine the efficiency of software as it relates to use of RAM. Software is measured against the functional requirements to evaluate fitness for purpose.

Part ofComputing ScienceSoftware design and development

Using selection statements

These two examples show extracts of code that make use of selection statements.

The inefficient version makes use of three separate IF statements. As the statements are not related to one another, the program will always execute each line of code. No matter what age is input, all nine lines of code will be executed.

The efficient version makes use of nested IF statements. When using a Nested IF, the first selection statement will run. If it finds that the age entered is higher than 65 then it will not run the second IF statement at all - the nested IF statement is only executed if the first selection statement does not assign a value to the ticketCategory variable.

InefficientEfficient
IF age ˃65 THENIF age ˃65 THEN
SET ticketCategory TO concession SET ticketCategory TO concession
END IFELSE
IF age˃= 18 AND age ˂=65 THEN IF age˃17 THEN
SET ticketCategory TO adult SET TICKETCategory TO adult
END IF ELSE
IF age˂18 THEN SET ticketCategory TO child
SET ticketCategory TO child END IF
END IFEND IF
InefficientIF age ˃65 THEN
EfficientIF age ˃65 THEN
Inefficient SET ticketCategory TO concession
Efficient SET ticketCategory TO concession
InefficientEND IF
EfficientELSE
InefficientIF age˃= 18 AND age ˂=65 THEN
Efficient IF age˃17 THEN
Inefficient SET ticketCategory TO adult
Efficient SET TICKETCategory TO adult
InefficientEND IF
Efficient ELSE
InefficientIF age˂18 THEN
Efficient SET ticketCategory TO child
Inefficient SET ticketCategory TO child
Efficient END IF
InefficientEND IF
EfficientEND IF