Selection

What Does Selection Mean in Programming?

In programming, selection means choosing which code runs based on a condition. See how it differs from sequence and iteration, and other uses of the word.

On this page
  1. Short answer
  2. The meaning in context
  3. How selection differs from sequence and iteration
  4. The common forms
  5. What a condition is
  6. Other meanings of “selection” in computing
  7. Key takeaways

Short answer

In programming, selection means a construct that picks one path from several, based on a condition. The program evaluates a condition, runs the matching block of code, and skips the others. The word comes from the idea of choosing which instructions to run.

The meaning in context

Programmers use the term for the decision point in a program. When the program reaches a selection statement, one branch runs and the others are skipped for that run. Different inputs can send the program down different branches, so the same code can produce different results.

Teachers and textbooks sometimes say “branching” or “conditional” for the same idea. The names vary, but the core is the same: a condition sends the program down one path.

How selection differs from sequence and iteration

Three ideas describe most program structure:

  • Sequence runs statements in order, one after another.
  • Selection chooses one path based on a condition.
  • Iteration repeats a block of statements.

A program with only sequence does the same steps every time. Selection adds decisions, and iteration adds repetition. Most real programs use all three.

The common forms

Selection statements in most languages come in a few forms:

  • if runs a block when a condition is true.
  • if-else runs one block when true and another when false.
  • else-if tests several conditions in order and runs the first block whose condition is true.
  • switch, or a similar form such as Python’s match (added in Python 3.10), compares one value against several fixed options.

The exact syntax differs from one language to another. The idea does not.

What a condition is

A condition is an expression that evaluates to true or false. It may compare values, such as score >= 60, or combine several checks with and, or and not. Once the condition is evaluated, the program takes exactly one path through the selection.

Here is a short example in Python:

temperature = 30

if temperature > 25:
    print("Warm")
else:
    print("Cool")

With 30, the program prints “Warm”. If the value were 20, it would print “Cool”. The code is the same, but the result changes with the input.

Other meanings of “selection” in computing

The word appears in other computing contexts with different meanings. Keeping them apart avoids confusion:

  • Selection sort is a sorting algorithm. It repeatedly finds the smallest remaining item and moves it into place. The name refers to that search step. For a broader comparison of sorting methods, see what is the most efficient sorting algorithm.
  • Selection in databases. In relational algebra, selection keeps only the rows that match a condition. In SQL, the WHERE clause does this job.
  • Selection in genetic algorithms. Here, candidate solutions are chosen to produce the next generation, usually based on how well they score. The idea borrows from natural selection.

None of these is a selection statement, although each one involves choosing.

Key takeaways

  • In programming, selection means choosing which code runs based on a condition.
  • It is one of three basic control structures, alongside sequence and iteration.
  • Common forms include if, if-else, else-if and switch-style statements.
  • The word also appears in sorting, databases and genetic algorithms, so check the context.

The complete guide to selection

What Is Selection in Computer Science?
Read the guide

Keep exploring

Guides from other topics

All topics →