B2.3.2 Construct programs utilizing appropriate selection structures.

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

OperatorMeaningExampleResult
<Less thanx < 5True if x is less than 5
<=Less than or equalx <= 5True if x is 5 or less
>Greater thanx > 5True if x is more than 5
>=Greater than or equalx >= 5True if x is 5 or more
==Equal tox == 5True if x is exactly 5
!=Not equal tox != 5True if x is not 5

Logical (Boolean) Operators

OperatorMeaningJava ExamplePython Example
&& / andLogical ANDx > 0 && y > 0x > 0 and y > 0
` /or`Logical OR
! / notLogical 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

PracticeDescription
Use indentation and bracesClearly separate each block of logic. Python requires indentation; Java uses {}.
Avoid deeply nested ifsUse compound conditions or helper functions to improve readability.
Use logical groupingCombine conditions with and, or, and not where appropriate.
Default cases (else)Always include an else if a decision chain is logically complete.
SimplifyRewrite complex conditions into intermediate Boolean variables when needed.

6. Common Errors and How to Avoid Them

Error TypeExampleExplanation
Missing else/elifOnly one branch handledLeads to skipped logic for other inputs
Incorrect comparisonif x = 5 (Java)Assignment instead of comparison (==)
Operator misuseand instead of && in JavaSyntax error due to language mismatch
Logic flawif x > 5 or x < 10Always true — should be and if range is intended
Redundant conditionselif x > 70 after x > 80Already checked; needs reordering

7. Summary

FeatureDescription
Selection controlDirects program flow based on conditions
Keywordsif, else, else if (Java), elif (Python)
Relational operators<, <=, >, >=, ==, !=
Logical operatorsand, or, not (Python), &&, `
Execution modelTop-down, conditionally branching based on truth-values
Typical usesInput 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.