Logic
Logic
Programs don't just calculate.
They also need to make decisions.
Questions like:
- Is the button pressed?
- Is the temperature too high?
- Is the distance less than 10 cm?
- Did the player score enough points?
The Logic category allows programs to ask questions and decide what to do next.
Think of Logic as the decision-maker inside your robot.
What Is Logic?
Logic blocks ask questions.
Every question has one of two answers:
TRUE
or
FALSE
Example:
5 > 3
↓
TRUE
2 > 10
↓
FALSE
Think of Logic as the brain of your program.
Logic Category
[INSERT CATEGORY IMAGE HERE: Logic]
Figure 3.1 – Blocks available in the Logic category.
The Logic category can be organized into:
COMPARE
↓
COMBINE
↓
REVERSE
↓
DECIDE
Logic Block Quick Reference
| Block | Purpose | Main Idea |
|---|---|---|
| Comparison | Compare values | Ask a question |
| Range Check | Check limits | Between values |
| AND | Both conditions | All must be true |
| OR | Either condition | One can be true |
| NOT | Reverse result | Opposite answer |
| TRUE/FALSE | Boolean values | Yes or No |
| If True / If False | Choose a value | Decision making |
| In / Not In | Check a list | Exists or not |
| To Boolean | Convert to TRUE/FALSE | Logical value |
Comparing Values
[INSERT BLOCK IMAGE HERE: Comparison]
Comparison blocks ask questions.
Comparison Options
| Question | Meaning |
|---|---|
| = | Equal to |
| ≠ | Not equal |
| < | Less than |
| ≤ | Less than or equal |
| > | Greater than |
| ≥ | Greater than or equal |
Examples
5 = 5
↓
TRUE
5 ≠ 5
↓
FALSE
10 > 3
↓
TRUE
Concept
Question
↓
TRUE/FALSE
Comparing Sensor Values
A sensor reading can be compared to a limit.
Example:
Temperature > 30
Result:
TRUE
or
FALSE
Concept
Sensor
↓
Compare
↓
Result
Range Checks
[INSERT BLOCK IMAGE HERE: Range Comparison]
Sometimes a value must stay between two limits.
Example:
0 < Score < 100
This asks:
Is the score between 0 and 100?
Concept
Lower Limit
↓
Value
↓
Upper Limit
TRUE and FALSE
[INSERT BLOCK IMAGE HERE: Boolean]
The most important Logic values are:
TRUE
and
FALSE
Think of Them As
TRUE = YES
FALSE = NO
Combining Conditions
[INSERT BLOCK IMAGE HERE: AND/OR]
Sometimes one question is not enough.
AND
AND requires both conditions to be true.
Example:
Button Pressed
AND
Distance < 10
Result:
TRUE
only if both are true.
Memory Tip
AND = BOTH
OR
OR requires only one condition to be true.
Example:
Button Pressed
OR
Remote Button Pressed
Memory Tip
OR = EITHER
AND vs OR
| Block | Meaning |
|---|---|
| AND | Both conditions |
| OR | One or both conditions |
Example
Rain
AND
Wind
↓
Need both
Rain
OR
Wind
↓
Need only one
NOT
[INSERT BLOCK IMAGE HERE: NOT]
NOT reverses the answer.
Examples
TRUE
↓
NOT
↓
FALSE
FALSE
↓
NOT
↓
TRUE
Memory Tip
NOT = OPPOSITE
Choosing Between Two Values
[INSERT BLOCK IMAGE HERE: If True / If False]
Sometimes we want one value if something is true and another value if it is false.
Example:
IF Temperature > 30
↓
HOT
ELSE
↓
OK
Concept
Question
↓
TRUE?
↙ ↘
Yes No
↓ ↓
Value A Value B
Checking a List
[INSERT BLOCK IMAGE HERE: In]
Logic can check if a value exists inside a list.
Example:
3 in [1,2,3,4]
↓
TRUE
8 in [1,2,3,4]
↓
FALSE
Converting to Boolean
[INSERT BLOCK IMAGE HERE: To Boolean]
This block converts a value into:
TRUE
or
FALSE
Concept
Value
↓
Boolean
↓
TRUE/FALSE
Logic and Control
Logic and Control work together.
Logic asks the question.
Control decides what to do.
Example:
Distance < 10
↓
TRUE
↓
Turn LED ON
Concept
QUESTION
↓
TRUE/FALSE
↓
ACTION
Logic and Sensors
Logic becomes most useful when used with sensors.
Example:
DHT11
↓
Temperature
↓
Compare
↓
Decision
Temperature Alarm
Temperature > 30
↓
TRUE
↓
Turn Buzzer ON
Obstacle Detector
Distance < 10
↓
TRUE
↓
Stop Robot
Temperature Warning System
Temperature
↓
> 30?
↓
TRUE
↓
LED ON
Skills:
Comparison
Sensors
Decisions
Obstacle Detector
Distance
↓
< 10?
↓
TRUE
↓
Buzzer
Skills:
Ultrasonic Sensor
Logic
Outputs
Score Checker
Score
↓
>= 10?
↓
TRUE
↓
Winner!
Skills:
Variables
Comparisons
Control
Quick Guide for Students
Need to compare values?
Use:
= ≠ < > ≤ ≥Need both conditions?
Use:
AND
Need only one condition?
Use:
OR
Need the opposite answer?
Use:
NOT
Need TRUE or FALSE?
Use:
Boolean
Need to check limits?
Use:
Range Comparison
Quick Reference
| Family | Main Idea |
|---|---|
| Comparison | Ask a question |
| Range | Check limits |
| AND | Both conditions |
| OR | Either condition |
| NOT | Opposite result |
| Boolean | TRUE/FALSE |
| Conditional Value | Choose between values |
| Membership | Check a list |
| Boolean Conversion | Convert to TRUE/FALSE |
Summary
In this lesson, you learned how Logic helps programs make decisions.
You explored:
Comparisons
TRUE and FALSE
AND
OR
NOT
Range Checks
Conditional Values
Membership Tests
Sensor Decisions
Logic helps programs decide what to do based on information they receive from sensors, variables, and user input.
Key Takeaway
Logic is the decision-maker inside your program. It asks questions, produces TRUE or FALSE answers, and helps your robot decide what action to take next.
