Selection

Programming Fundamentals
structure

Also known as: conditional, if-then, branching

Grade 6-8

View on concept map

Choosing which block of code to execute based on whether a condition is true or false. Selection enables programs to respond differently to different situations.

Definition

Choosing which block of code to execute based on whether a condition is true or false. Selection allows programs to make decisions, following one path when a condition holds and a different path otherwise, using constructs like if, else-if, and else.

💡 Intuition

If this is true, do that path. If it is false, take a different path instead.

🎯 Core Idea

Programs can choose between different execution paths depending on the current data values.

Example

IF temperature > 30°C THEN turn on AC, ELSE turn off AC. Two different paths, one condition.

🌟 Why It Matters

Selection enables programs to respond differently to different situations. Without conditionals, every program would do the exact same thing every time it runs, making interactive software, error handling, and decision-based logic impossible.

💭 Hint When Stuck

When writing a conditional, first identify the exact condition to test and make sure it evaluates to true or false. Then write the code for the 'true' branch, and consider whether you need an 'else' branch for the false case. Always check boundary values where the condition flips.

Formal View

Selection evaluates a boolean expression B and executes statement block S_1 if B is true, or optionally S_2 if B is false: IF B THEN S_1 ELSE S_2.

🚧 Common Stuck Point

Conditions must be unambiguous—what happens when temperature = 30?

⚠️ Common Mistakes

  • Using assignment (=) instead of comparison (==) in the condition, which silently succeeds in some languages
  • Forgetting to handle the else case, leaving unexpected inputs with no defined behavior
  • Writing overlapping conditions in if/else-if chains so that the wrong branch executes

Frequently Asked Questions

What is Selection in CS Thinking?

Choosing which block of code to execute based on whether a condition is true or false. Selection allows programs to make decisions, following one path when a condition holds and a different path otherwise, using constructs like if, else-if, and else.

When do you use Selection?

When writing a conditional, first identify the exact condition to test and make sure it evaluates to true or false. Then write the code for the 'true' branch, and consider whether you need an 'else' branch for the false case. Always check boundary values where the condition flips.

What do students usually get wrong about Selection?

Conditions must be unambiguous—what happens when temperature = 30?

How Selection Connects to Other Ideas

To understand selection, you should first be comfortable with sequence. Once you have a solid grasp of selection, you can move on to boolean logic and nested conditionals.

💻 Interactive Visualization

Temperature: 50°C

Adjust temperature to see different code paths execute