IF-THEN-ELSE: The most common structure, where a block of code runs if a condition is true, and an optional 'else' block runs if it is false.
SWITCH-CASE: A multi-way selection structure used when a single variable is compared against multiple constant values, improving readability over multiple nested IFs.
Nested Decisions: Placing one decision structure inside another to handle hierarchical logic or multiple dependent criteria.
Flowcharts: A visual design tool where diamonds represent decision points, providing a clear map of the program's logic flow.
| Feature | IF-THEN-ELSE | SWITCH-CASE |
|---|---|---|
| Flexibility | Can evaluate complex ranges and multiple variables. | Usually limited to checking a single variable against discrete values. |
| Readability | Can become cluttered with many 'else if' levels. | Highly readable for many specific outcomes of one variable. |
| Logic Type | Best for Boolean logic and inequalities (, ). | Best for equality checks against a list of constants. |
Sequential Decisions: Multiple independent IF statements where every condition is checked regardless of previous outcomes.
Mutually Exclusive Decisions: Using 'ELSE IF' structures where only the first true condition's block is executed, skipping the rest.
Boundary Value Analysis: Always check the 'edge cases' of your conditions, such as testing exactly if the condition is .
Trace Tables: Use a trace table to manually track variable values through a decision structure to ensure the logic behaves as expected.
De Morgan's Laws: Be aware of how to simplify complex NOT conditions, as examiners often use nested negations to test logical fluency.
Logical Completeness: Ensure that all possible input values are handled, often by including a 'default' case in a switch or a final 'else' in an if-chain.
Assignment vs. Equality: A frequent error is using the assignment operator () instead of the equality comparison operator () inside a condition.
Overlapping Conditions: In a series of IF-ELSE statements, if conditions overlap (e.g., and ), the order matters significantly.
Logic Shadowing: This occurs when an outer condition is so broad that an inner nested condition can never be reached, creating 'dead code'.