Decisions in Computational Thinking (OCR A-Level Computer Science): Revision Notes
Decisions in Computational Thinking
Overview
In computational thinking, decisions are critical points where a programme must choose between different actions based on specific conditions. These decisions influence the flow of control, determining which path the programme follows. Understanding where and how decisions are made is essential for designing effective algorithms and programmes.
What are Decisions in a Programme?
- Definition: A decision is a point in a programme where the next action depends on whether a certain condition is true or false.
- Purpose: To control the programme's flow by choosing between different branches or paths.
Decision Structures:
IF Statements:
Used to execute a block of code if a condition is true.
Example:
IF temperature > 30 THEN
PRINT "It's hot"
ELSE
PRINT "It's not hot"
ENDIF
IF-ELSE-IF Ladder:
Handles multiple conditions.
Example:
IF score >= 90 THEN
PRINT "Grade A"
ELSEIF score >= 80 THEN
PRINT "Grade B"
ELSE
PRINT "Grade C"
ENDIF
CASE/SWITCH Statements:
Used when multiple conditions are checked against a single variable.
Example:
CASE day OF
"Monday": PRINT "Start of work week"
"Friday": PRINT "End of work week"
"Saturday", "Sunday": PRINT "Weekend"
ENDCASE
LOOPS with Conditions:
Decisions are also made within loops using WHILE, FOR, or REPEAT constructs.
Example:
WHILE attempts < 3 AND password != correct_password
INPUT password
attempts = attempts + 1
ENDWHILE
Identifying Decision Points in a Solution
- Understand the Problem Requirements: Identify the conditions or criteria that affect the programme's behaviour.
- Locate Decision Points:
- Points where the programme must choose between different actions based on conditions.
- Common scenarios:
- Validating user input.
- Determining programme outputs based on input data.
- Handling different cases in a process.
- Analyse Flow Control: Decisions create branches or loops, altering the programme's execution path.
Example Scenarios
Scenario 1: ATM Machine Problem: Dispense cash if the user's account has sufficient funds.
Decision Points:
- Check if the account balance is greater than or equal to the withdrawal amount.
- If yes, dispense cash; if no, display an error message. Pseudocode:
IF balance >= withdrawal_amount THEN
DISPENSE cash
UPDATE balance
ELSE
PRINT "Insufficient funds"
ENDIF
Scenario 2: Traffic Light System Problem: Change the light based on the current state and timer.
Decision Points:
- Check the current light state.
- Transition to the next light after a timer expires. Flowchart Decision Example:
- IF light is green → change to yellow.
- IF light is yellow → change to red.
- IF light is red → change to green.
Scenario 3: Online Quiz Application Problem: Provide feedback based on the user's score.
Decision Points:
- Determine the feedback message based on the score. Pseudocode:
IF score >= 80 THEN
PRINT "Excellent"
ELSEIF score >= 50 THEN
PRINT "Good"
ELSE
PRINT "Needs Improvement"
ENDIF
How Decisions Affect Programme Flow
- Branching:
- Decisions create multiple branches in the programme, leading to different outcomes based on conditions.
Example: IF statements.
- Looping:
- Decisions within loops determine how many times the loop will execute.
Example: WHILE loops depend on a condition to continue or terminate.
- Early Exits:
- Decisions can lead to the early termination of a programme or function.
Example: Exiting a loop or returning from a function when a condition is met.
- Multiple Outcomes:
- Different decision paths can lead to varying outputs or behaviours depending on the inputs.
Tools for Representing Decisions
Flowcharts:
- Visual representation of decision points and their corresponding paths.
- Decision Symbol (Diamond): Represents a decision point.
Example: Start → Input Temperature → Decision: Is Temperature > 30? → Yes: Print "It's hot" → No: Print "It's not hot" → End.
Pseudocode:
- Describes decision-making processes in a structured, easy-to-read format.
- Useful for planning before coding.
Code Implementation:
- Translate decisions into actual programme code using decision structures like IF, CASE, or loops.
Note Summary
Common Mistakes
- Misplacing Decision Points: Placing decisions at the wrong point in the flow can lead to incorrect outcomes.
- Ignoring Edge Cases: Failing to account for all possible conditions or inputs (e.g., boundary values, unexpected input).
- Overcomplicating Decision Logic: Using too many nested IF statements can make the programme harder to understand and debug.
- Missing Default Cases: In CASE statements, omitting a DEFAULT case can lead to unhandled scenarios.
Key Takeaways
- Decisions are critical points in a programme where the flow of control depends on conditions.
- They influence programme flow by introducing branches, loops, and conditional paths.
- Use tools like pseudocode, flowcharts, and code to plan and implement decision-making logic.
- Properly placed and designed decisions ensure programmes handle all possible scenarios efficiently and correctly.