Computational Thinking: Logic and Selection

CSC1EA1 - Computational Thinking

Logic and Selection

Overview
  • Focus on Boolean values, if blocks, and nested if blocks.

Boolean Values
  • Definition: Boolean variables or expressions can only take on one of two possible values: True / False.

  • Examples:

    • When clicked and the condition is true, the response is: "The condition was met".
    • When clicked and the condition is false, the response remains "The condition was met".

Boolean Operations
  • Combination: Boolean values can be combined using Boolean operators.

  • Common Boolean Operators:

    • AND
    • OR
    • NOT
  • Truth Tables for AND and OR Operators:

    • AND (Intersection A ∩ B):
    • Operand A | Operand B | Result
    • -----------|------------|--------
    • False | False | False
    • False | True | False
    • True | False | False
    • True | True | True
    • OR (Union A ∪ B):
    • Operand A | Operand B | Result
    • -----------|------------|--------
    • False | False | False
    • False | True | True
    • True | False | True
    • True | True | True
    • NOT:
    • Operand | Result
    • ---------|--------
    • False | True
    • True | False

Inclusive vs Exclusive OR
  • Inclusive OR: The operator is known as an inclusive OR operator. This is commonly denoted as AND/OR in verbal communication. The truth table shown previously demonstrates this.
  • Exclusive OR (XOR):
    • The XOR operator returns True when its operands are different and False when they are the same.
    • Truth Table for XOR:
    • Operand A | Operand B | Result
    • -----------|------------|--------
    • False | False | False
    • False | True | True
    • True | False | True
    • True | True | False

De Morgan's Law
  • If the NOT operator is applied to a Boolean expression, then:
    • Each Boolean variable is negated.
    • Every AND becomes an OR.
    • Every OR becomes an AND.

Logical Circuits
  • Logical circuits are represented using a specific notation:
    • AND gate
    • OR gate
    • NOT gate
    • EXCLUSIVE-OR gate
    • Representation of XOR: XOR=(AextORB)extANDNOT(AextANDB)XOR = (A ext{ OR } B) ext{ AND NOT}(A ext{ AND } B)

Programming in Snap!
  • In Snap!, the blocks for the Boolean operators are aligned with their English equivalents: AND, OR, NOT. XOR will be created manually.

Example: Voting Eligibility in Utopia
  • Criteria for enfranchisement:
    1. Must be a citizen of Utopia
    2. Must be over 18 years of age
    3. Must not have a criminal record
  • Implementation:
when clicked  
set isOver18 to true  
set isCitizen to true  
set isCriminal to false  
set isEnfranchised to isOver18 AND isCitizen AND NOT isCriminal  
if isEnfranchised then  
  say "Can vote"  
end if  

If Blocks
  • Definition: Provides selection (choice) of actions to take based on a given condition.
  • Structure of an If-statement:
If (condition) Then  
  Sequence of steps to be executed because condition is True  
[ Else ]  
  Sequence of steps to be executed because condition is False  
End If  
  • Examples:
    • Example 1:
      If GrossIncome < 10000 Then Tax amount is 15% Else Tax amount is 25% End If
    • Example 2:
      If Service = "Good" Then Write good review Else Complain to management End If

Types of If Blocks
  • Single branch If:
    • Executes actions based on whether the condition is true or false.
  • Two Branch (If-Else):
    • If condition is true, perform true actions; if false, perform remaining actions.
  • Multi-way Branch (If-ElseIf / Select):
    • Evaluates multiple conditions.
    • [ Example Workflow ]:
    • If condition 1 is true - perform condition 1 actions.
    • If condition 1 is false but condition 2 is true - perform condition 2 actions.
    • Continue until conditions are exhausted.
    • Perform remaining actions if no conditions are met.

Exam Access Determination Example
  • Criteria:
    • Semester mark must be above 40.
    • Practical component mark must be above 50.
  • Implementation:
when clicked  
ask "Enter semester mark" and wait  
set sm to answer  
ask "Enter practical component mark" and wait  
set pcm to answer  
if sm > 39 AND pcm > 49 then  
  say "Access to exam granted"  
else  
  say "No access to exam"  
end if  

Nested If Blocks Example
  • Criteria for final result:
    • 75 or more is a distinction.
    • Between 50 and 74 is a pass.
    • Between 45 and 49 is an SSA.
    • Between 0 and 44 is a failure.
  • Implementation:
    • Logic for graded distinctions broken down using nested if blocks.

Summary of Code Structures
  • Sequence:
    • Step 1
    • Step 2
    • Step n
  • Selection:
IF (condition) THEN  
ELSE  
END IF  
  • Repetition:
WHILE (condition)  
END WHILE  
  • Iteration: Use selection, choice, and repetition rigorously to formulate structured decision trees in code.

Additional Notes
  • Consider visual aids and logical diagrams for understanding Boolean operations and logical circuits.
  • Engage with practical exercises to enhance comprehension and application of these concepts in computational thinking.