Short answer
In computer science, selection is the way a program chooses which code to run. It checks a condition, and the result decides which path the program follows. If the condition is true, one block runs. If it is false, a different block runs, or nothing does. Selection is one of the basic building blocks of programs, alongside sequence and iteration.
Sequence, selection and iteration
Most programs can be described with three basic control structures:
- Sequence runs statements one after another, in the order they are written.
- Selection chooses between paths based on a condition.
- Iteration repeats a block while a condition holds, or for a set number of times.
Selection is the one that lets a program make decisions. Without it, a program would run the same steps in the same order, whatever its input.
How a condition decides the path
A condition is an expression that evaluates to true or false. The most common conditions compare two values: is the score at least 60, is the username equal to “admin”, is the list empty. Comparison operators such as ==, !=, <, >, <= and >= make these checks. Logical operators such as and, or and not combine several checks into one condition.
The program does not need to understand the condition in any deeper sense. It only needs to evaluate it and take the matching branch.
If, if-else and else-if
Selection usually appears in three forms.
If runs a block only when the condition is true:
if score >= 60
print "Passed"
If-else gives two paths, one for true and one for false:
if score >= 60
print "Passed"
else
print "Not passed"
Else-if adds more checks, tested in order. The first true condition wins:
if score >= 90
print "A"
else if score >= 80
print "B"
else
print "Below B"
Order matters in an else-if chain. A score of 95 gets “A” only because that check comes first. If the checks were reversed, the same score would match the 80 check and get “B” instead.
A worked example
Suppose a shop gives a 10 percent discount on orders over 100. In Python:
total = 120
if total > 100:
total = total * 0.9
print("Discount applied")
else:
print("No discount")
print("Total:", total)
With a total of 120, the condition is true. The discount line runs, the program prints “Discount applied”, and the total becomes 108. Change the starting total to 80, and the condition is false. The else block runs, the program prints “No discount”, and the total stays at 80. The last line runs in both cases, because it sits outside the selection.
Switch and multiway selection
When one value is compared with several fixed options, many languages offer a switch statement. C, Java and JavaScript use switch, with case labels and, usually, break statements. Python 3.10 added match, which works in a related way but can do more. Behavior differs between languages, so check the language reference before relying on one.
Nested selection
A selection can sit inside another. For example, a program might first check whether a user is logged in, then check whether that user is an administrator. Nesting is useful, but deep nesting gets hard to read. Often a single condition joined with and, or an early return, makes the logic clearer.
Common mistakes
Some mistakes come up again and again:
- Using assignment where comparison is meant. In languages such as C and JavaScript,
if (x = 5)assigns 5 toxinstead of comparing. Use==for comparison. Python rejects this form with a syntax error. - Leaving out the else. A program may quietly do nothing for a case you did not expect.
- Picking the wrong boundary. Using
>where>=is meant changes the result for the exact boundary value. Test the boundary itself, not only values on either side of it. - The dangling else. In some C-style languages without braces, an else attaches to the nearest if. Braces make the intended pairing clear.
- Checks that can never run. An else-if that repeats an earlier condition is never reached.
Selection in algorithms
An algorithm is a set of steps for solving a problem. Selection lets one algorithm handle different inputs or states. A search might stop as soon as it finds a match. A sorting method might swap two items only when they are out of order.
Selection also appears inside well-known algorithms. Selection sort, for example, repeatedly looks for the smallest item in the unsorted part of a list and moves it into place. Its name comes from that selecting step, so it is a different idea from selection statements. For a broader look at which sorting methods are fastest and when, read about what is the most efficient sorting algorithm.
The order of steps that selection builds on is covered in what is sequencing in computer science.
Logic behind conditions
The true-or-false reasoning in a condition belongs to a wider body of material. Logic and sets are part of what is discrete structures in computer science, and those topics explain why conditions combine the way they do.
Key takeaways
- Selection lets a program choose a path based on a condition.
- If, if-else and else-if cover most cases. Switch-style forms suit one value with many fixed options.
- Conditions are true-or-false expressions, built from comparisons and logical operators.
- Watch for missing else branches, wrong boundaries and unclear nesting.
- Most algorithms are built from sequence, selection and iteration together.