selectionA decision within a computer program when the program decides to move on based on the results of an event. is a programming concept where a section of code is run only if a conditionIn computing, this is a statement that is either true or false. A computation depends on whether a condition equates to true or false. is met. 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 determines which path the programSequences of instructions for a computer. 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 executionThe process of a program being run on a computer. one set of statementThe smallest element of a programming language which expresses an action to be carried out.. Otherwise, it would follow a different path and execute a different set of statements.
An explanation of selection, as used in algorithms and programming
Selection works by testing a condition. The test gives a BooleanA data type in computing which only has two possible values, true or false. result - TRUE or FALSE.
In programming, selection is implemented using IF THEN ELSE statements:
OUTPUT “How old are you?”
age ← USERINPUT
IF age > 16 THEN OUTPUT "You are old enough to drive a car"
ELSE OUTPUT "Come back when you are older!"
ENDIF
In the above pseudocode Also written as pseudo-code. A method of writing up a set of instructions for a computer program using plain English. This is a good way of planning a program before coding. program, the path the program takes depends on the condition. A variableA memory location within a computer program where values are stored., 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 outputs to 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 outputs to the user that they are not yet old enough to drive.