B2.3.2 Construct programs utilizing appropriate selection structures.
• Must include: if, else, else if (Java), elif (Python), to execute different code blocks based on specified conditions
• Selection structures with or without Boolean operators (AND, OR, NOT) and/or relational operators (<, <=, >, >=, ==, !=) to control program flow effectively
The Big Idea
In programming, selection structures allow a program to make decisions—to choose different paths of execution based on conditions. At runtime, these decisions determine which blocks of code will be executed and which will be skipped. This is a critical component of control flow, allowing programs to behave differently under different inputs, states, or environments.
Selection structures are implemented using conditional statements such as if, else if (Java), elif (Python), and else. These structures evaluate Boolean expressions, often composed using comparison operators (<, >, ==, etc.) and logical operators (AND, OR, NOT), to direct the flow of execution. Please understand that the term relational operator is the same as comparison operator.
1. Types of Selection Structures
A. Single Selection (if)
Executes a block only if a condition is true.
Java:
if (score > 90) {
System.out.println("Excellent");
}
Python:
if score > 90:
print("Excellent")
B. Double Selection (if-else)
Executes one block if the condition is true, another if false.
Java:
if (score >= 60) {
System.out.println("Pass");
} else {
System.out.println("Fail");
}
Python:
if score >= 60:
print("Pass")
else:
print("Fail")
C. Multiple Selection (if-else if-else in Java, if-elif-else in Python)
Allows multiple mutually exclusive conditions to be evaluated in sequence.
Java:
if (score >= 90) {
System.out.println("A");
} else if (score >= 80) {
System.out.println("B");
} else if (score >= 70) {
System.out.println("C");
} else {
System.out.println("Fail");
}
Python:
if score >= 90:
print("A")
elif score >= 80:
print("B")
elif score >= 70:
print("C")
else:
print("Fail")
2. Boolean and Relational Operators in Conditions
Selection statements depend on Boolean expressions. These expressions are formed using:
Relational Operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
< | Less than | x < 5 | True if x is less than 5 |
<= | Less than or equal | x <= 5 | True if x is 5 or less |
> | Greater than | x > 5 | True if x is more than 5 |
>= | Greater than or equal | x >= 5 | True if x is 5 or more |
== | Equal to | x == 5 | True if x is exactly 5 |
!= | Not equal to | x != 5 | True if x is not 5 |
Logical (Boolean) Operators
| Operator | Meaning | Java Example | Python Example |
|---|---|---|---|
&& / and | Logical AND | x > 0 && y > 0 | x > 0 and y > 0 |
| ` | /or` | Logical OR | |
! / not | Logical NOT | !(x > 0) | not x > 0 |
3. Nested Selection
Selection statements can be nested inside one another to form more complex decision structures.
Python Example:
if age >= 18:
if has_id:
print("You may enter.")
else:
print("ID required.")
else:
print("You must be 18 or older.")
Nested conditionals can be powerful but should be used sparingly to avoid excessive complexity and poor readability.
4. Program Design Using Selection Structures
The goal is to control the flow of execution based on meaningful decisions derived from program input, user behavior, or internal state.
Example: Grade Classification Program (Python)
grade = int(input("Enter your grade: "))
if grade >= 90:
print("A")
elif grade >= 80:
print("B")
elif grade >= 70:
print("C")
elif grade >= 60:
print("D")
else:
print("F")
Example: Login Access Check (Java)
String username = "admin";
String password = "pass123";
if (inputUser.equals(username) && inputPass.equals(password)) {
System.out.println("Access granted.");
} else {
System.out.println("Access denied.");
}
5. Best Practices for Selection Structures
| Practice | Description |
|---|---|
| Use indentation and braces | Clearly separate each block of logic. Python requires indentation; Java uses {}. |
| Avoid deeply nested ifs | Use compound conditions or helper functions to improve readability. |
| Use logical grouping | Combine conditions with and, or, and not where appropriate. |
| Default cases (else) | Always include an else if a decision chain is logically complete. |
| Simplify | Rewrite complex conditions into intermediate Boolean variables when needed. |
6. Common Errors and How to Avoid Them
| Error Type | Example | Explanation |
|---|---|---|
| Missing else/elif | Only one branch handled | Leads to skipped logic for other inputs |
| Incorrect comparison | if x = 5 (Java) | Assignment instead of comparison (==) |
| Operator misuse | and instead of && in Java | Syntax error due to language mismatch |
| Logic flaw | if x > 5 or x < 10 | Always true — should be and if range is intended |
| Redundant conditions | elif x > 70 after x > 80 | Already checked; needs reordering |
7. Summary
| Feature | Description |
|---|---|
| Selection control | Directs program flow based on conditions |
| Keywords | if, else, else if (Java), elif (Python) |
| Relational operators | <, <=, >, >=, ==, != |
| Logical operators | and, or, not (Python), &&, ` |
| Execution model | Top-down, conditionally branching based on truth-values |
| Typical uses | Input validation, state decisions, access control, behavior switching |
Final Thoughts
Selection structures are at the heart of all non-linear program behavior. They allow code to make intelligent decisions, responding to real-world conditions, user input, and logical state. Writing good selection structures requires more than just syntax—it requires careful logical planning, truth-table reasoning, and a clear understanding of execution flow. A well-structured decision chain can make the difference between an elegant solution and an unmaintainable mess.