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.
| Inefficient | Efficient |
| IF age ˃65 THEN | IF age ˃65 THEN |
| SET ticketCategory TO concession | SET ticketCategory TO concession |
| END IF | ELSE |
| 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 IF | END IF |
| Inefficient | IF age ˃65 THEN |
|---|---|
| Efficient | IF age ˃65 THEN |
| Inefficient | SET ticketCategory TO concession |
|---|---|
| Efficient | SET ticketCategory TO concession |
| Inefficient | END IF |
|---|---|
| Efficient | ELSE |
| Inefficient | IF age˃= 18 AND age ˂=65 THEN |
|---|---|
| Efficient | IF age˃17 THEN |
| Inefficient | SET ticketCategory TO adult |
|---|---|
| Efficient | SET TICKETCategory TO adult |
| Inefficient | END IF |
|---|---|
| Efficient | ELSE |
| Inefficient | IF age˂18 THEN |
|---|---|
| Efficient | SET ticketCategory TO child |
| Inefficient | SET ticketCategory TO child |
|---|---|
| Efficient | END IF |
| Inefficient | END IF |
|---|---|
| Efficient | END IF |