大象传媒

Algorithms - EdexcelSelection

Algorithms are step-by-step plans for solving problems. Algorithms can be designed using pseudo-code, flowcharts, written descriptions and program code. There are also some standard algorithms for searching and sorting.

Part of Computer SciencePrinciples of computer science

Selection

is the second programming construct. In programming, there are occasions when a decision needs to be made. Selection is the process of making a decision. The result of the decision decides which path the will take next.

For example, a program could tell a user whether they are old enough to learn how to drive a car. If the user's age meets the required driving age, the program would follow one path and execute one set of . Otherwise, it would follow a different path and execute a different set of instructions.

An explanation of selection, as used in algorithms and programming

Selection works by testing a condition. The test gives a Boolean result - TRUE or FALSE. If the result is TRUE, the program follows one path - otherwise it follows another.

In programming, selection is implemented using IF THEN or IF THEN ELSE statements:

SEND 鈥楬ow old are you?鈥 TO DISPLAY
                    RECEIVE age FROM (INTEGER) KEYBOARD
                    IF age > 16 THEN
                    聽聽聽聽聽SEND 鈥榊ou are old enough to drive a car!鈥
                    聽聽聽聽聽TO DISPLAY
                    ELSE
                    聽聽聽聽聽SEND 鈥楥ome back when you are older!鈥
                    聽聽聽聽聽TO DISPLAY
                    END IF

In the program above, the path the program takes depends on the condition. A variable - in this case age - is used to test the condition.

If the value of age is greater than 16, the result of the tested condition is TRUE and the program follows the first path, which follows the statement THEN. This path informs the user that they are old enough to drive.

If the value of age is less than 16, the result is FALSE and the program follows the second path, which follows the statement ELSE. This path informs the user that they are not yet old enough to drive.

The statement END IF ends the selection.