Hiring Partners
Segilola Resources Operating LimitedTaviatech TechnologiesFCMBMTN NigeriaAdvans La Fayette Microfinance BankAboki AfricaFirst Bank of NigeriaOPay
โ† Certified Racket AI Expert ยท Lesson 3 of 8

M0dule Two

๐Ÿ“– Every lesson in this course is free to read right here, no account needed. Create a free account to track your progress, take the exam, and earn your certificate.
3
Module 2 ยท Racket AI Specialist โ€“ Decisions & Logic

๐Ÿง  Module Two ยท Decisions, Logic & Smarter Programs

Certified Racket AI Specialist ยท Building Intelligence

๐Ÿ“– Module Introduction

Hello again, young programmer! ๐Ÿ‘‹ In Module One, we learned how to talk to the computer using Racket. We used numbers, words, and even made our own commands. But a computer that only adds numbers is like a robot that can only wave โ€” useful, but not very smart.

In this module, we will teach our computer to think and make decisions. We will learn about logic โ€” the rules of true and false. We will also learn how to compare things, make choices, and repeat actions. By the end, your programs will be able to decide what to do based on information, just like a real AI!

๐Ÿค– Did you know? The "AI" in Artificial Intelligence is all about making computers that can make decisions, just like we do!

๐ŸŽฏ Learning Objectives

After this module, you will be able to:

  • โœ”๏ธ Understand what true and false mean in Racket.
  • โœ”๏ธ Compare numbers and words using operators like >, <, =.
  • โœ”๏ธ Write if statements that make decisions.
  • โœ”๏ธ Use cond (a multi-way if) to handle many choices.
  • โœ”๏ธ Combine conditions with and, or, not.
  • โœ”๏ธ Create programs that respond differently based on input.
  • โœ”๏ธ Think logically and break down big decisions into steps.

๐Ÿ“š Warm-up Story ยท The Magic Lamp

Imagine you find a magic lamp. When you rub it, a genie appears. But this genie is very strict โ€” it only follows rules. You tell the genie: "If I say the magic word, give me a gift. Otherwise, do nothing." The genie checks: did you say the word? If yes, it gives you a gift. If no, it stays still.

That is exactly what an if statement does in Racket. It checks a condition. If the condition is true, it does one thing. If false, it does something else (or nothing).

In this module, we will give our programs the power to make decisions like that genie. We'll also learn how to combine decisions โ€” like: "If it's raining AND I have an umbrella, go out. Otherwise, stay inside."

   +-------------------+
   |   Condition       |
   | (is it true?)     |
   +--------+----------+
            |
     +------+------+
     |             |
    YES           NO
     |             |
     V             V
  Do action A   Do action B
  

๐Ÿงฉ Main Lessons

Lesson 1 ยท True and False โ€“ The Building Blocks of Logic

Definition: In Racket, true and false are values that tell us if something is correct or not. We write #t for true and #f for false.

Why important: Every decision a computer makes is based on true or false. If it's true, do one thing. If false, do another.

Simple explanation: Think of a light switch. On = true, Off = false. The computer can only be in one of these two states for any question.

Real-life example: "Is the sky blue?" โ€“ yes (true). "Is water dry?" โ€“ no (false).

School example: "Is 2 + 2 equal to 4?" โ€“ true. "Is 5 less than 3?" โ€“ false.

Home example: "Is it time for dinner?" โ€“ maybe true or false depending on the clock.

Nigerian example: "Is Lagos a city in Nigeria?" โ€“ true. "Is Abuja in Ghana?" โ€“ false.

Illustration:

   #t   โ†’  True (yes, correct)
   #f   โ†’  False (no, not correct)

   ( = 5 5 )  โ†’  #t
   ( = 5 7 )  โ†’  #f
  
๐Ÿ“Œ Mini summary: True and false are like yes/no answers. Racket uses #t and #f.

Lesson 2 ยท Comparing Numbers ( > , < , = )

Definition: We compare numbers using operators: > (greater), < (less), = (equal), >= (greater or equal), <= (less or equal).

Why important: AI uses comparisons all the time โ€“ e.g., if a player's score is higher than the high score, update it.

Simple explanation: These are like the maths signs you know: > means bigger than, < means smaller than, = means same as.

Real-life: "Your age is greater than 10?" โ€“ check if you are older than 10.

School: "Is 80 greater than 70?" โ€“ yes, so you get a better grade.

Home: "Do we have more than 5 apples?" โ€“ check the count.

Nigerian: "Is the price of groundnut oil less than 2000 Naira?" โ€“ check to buy.

Illustration:

   (> 10 5)   โ†’  #t   (10 is greater than 5)
   (< 4 9)    โ†’  #t   (4 is less than 9)
   (= 7 7)    โ†’  #t   (7 is equal to 7)
   (>= 8 8)   โ†’  #t   (8 is greater or equal to 8)
  
๐Ÿ“Œ Mini summary: Comparison operators test numbers and return #t or #f.

Lesson 3 ยท Comparing Strings (equal? and string=?)

Definition: To compare words (strings), we use equal? or string=? to check if they are exactly the same.

Why important: AI often needs to check if a user said a certain word, e.g., "yes" or "no".

Simple explanation: It's like asking: "Is your name the same as my friend's name?"

Real-life: If you type "hello", the computer checks if that's equal to "hello".

School: "Is the answer 'B'?" โ€“ check the string.

Home: "Did you say 'please'?" โ€“ check the word.

Nigerian: "Did you say 'Abuja'?" โ€“ check the city name.

Illustration:

   (equal? "hello" "hello")  โ†’  #t
   (equal? "hello" "goodbye")โ†’  #f
   (string=? "Ade" "Ade")    โ†’  #t
  
๐Ÿ“Œ Mini summary: Use equal? or string=? to check if two strings are the same.

Lesson 4 ยท The If Statement โ€“ Making Decisions

Definition: An if statement checks a condition. If the condition is true, it does the first thing; if false, it does the second thing.

Why important: This is the most common way to make decisions in programs.

Simple explanation: If it's raining, take an umbrella; otherwise, don't.

Real-life: If you are hungry, eat. Otherwise, play.

School: If you have homework, do it; otherwise, watch TV.

Home: If the light is on, turn it off; else, leave it.

Nigerian: If you have money, buy meat pie; otherwise, buy puff-puff.

Illustration:

   (if ( > 5 3 )    ; condition: 5 > 3 ?
       "yes, it is"  ; true branch
       "no, it isn't") ; false branch
   โ†’ "yes, it is"
  
๐Ÿ“Œ Mini summary: If lets the computer choose between two actions based on a condition.

Lesson 5 ยท The Cond Statement โ€“ Many Choices

Definition: cond is like an if statement but with many choices. It checks each condition one by one, and runs the first that is true.

Why important: Sometimes we have more than two options โ€“ like choosing a meal from a menu.

Simple explanation: It's like a school grading system: if score โ‰ฅ 90 โ†’ A, else if โ‰ฅ 80 โ†’ B, else โ†’ C.

Real-life: Traffic light: red โ†’ stop, yellow โ†’ wait, green โ†’ go.

School: If you get 100, you get a star; if 80, a check; else, try again.

Home: If day is Monday, eat rice; Tuesday, eat yam; otherwise, eat beans.

Nigerian: If you are in Lagos, take the bus; if in Abuja, take a taxi; else, walk.

Illustration:

   (cond
     [(> 90 100) "A"]   ; if false, skip
     [(> 75 100) "B"]   ; false
     [else "C"])        ; else runs if nothing else true
   โ†’ "C"
  
๐Ÿ“Œ Mini summary: Cond is for multiple options. It picks the first true condition.

Lesson 6 ยท Combining Conditions with AND, OR, NOT

Definition: We can combine true/false values using and, or, not. and is true only if all are true; or is true if at least one is true; not flips true to false and false to true.

Why important: Real decisions often need multiple conditions: "If it's sunny AND I'm free, I'll play outside."

Simple explanation: AND: both must be true. OR: at least one true. NOT: the opposite.

Real-life: You can watch TV if you finish homework AND your mom says yes.

School: You get a prize if you score 100 OR you are the best student.

Home: You can eat if the food is ready AND you set the table.

Nigerian: You can go to the market if you have money AND it's not raining.

Illustration:

   (and #t #t)   โ†’  #t
   (and #t #f)   โ†’  #f
   (or #t #f)    โ†’  #t
   (or #f #f)    โ†’  #f
   (not #t)      โ†’  #f
   (not #f)      โ†’  #t
  
๐Ÿ“Œ Mini summary: AND, OR, NOT help us build complex conditions.

Lesson 7 ยท Using Logic to Make a Smart Calculator

Let's build a calculator that can add, subtract, multiply, or divide based on the operator you choose. We'll use cond to pick the operation.

   (define (calculate op a b)
     (cond
       [(equal? op "add") (+ a b)]
       [(equal? op "sub") (- a b)]
       [(equal? op "mul") (* a b)]
       [(equal? op "div") (/ a b)]
       [else "unknown operation"]))
   (calculate "add" 5 3)   โ†’ 8
   (calculate "mul" 4 2)   โ†’ 8
  
๐Ÿ“Œ Mini summary: We can use logic to choose between many actions.

Lesson 8 ยท The Number of Choices โ€“ Using else

Definition: In cond, we can use else as the last option to catch anything that wasn't caught earlier.

Why important: It's like a safety net โ€“ if nothing matches, do this.

Simple explanation: "If you have A, good; if B, okay; else, try again."

Real-life: If you have a ticket, enter; else, buy one.

School: If you answer all questions, you pass; else, you need to study more.

Home: If you finish your vegetables, you get dessert; else, no dessert.

Nigerian: If the bus arrives, board; else, wait.

Illustration:

   (cond
     [(= 5 6) "not"]
     [else "everything else"])
   โ†’ "everything else"
  
๐Ÿ“Œ Mini summary: else catches all remaining cases.

Lesson 9 ยท Nesting If โ€“ Decisions Inside Decisions

Definition: You can put an if or cond inside another one. This is called nesting.

Why important: Some decisions depend on other decisions, like: "If you have money, then if the shop is open, buy something."

Simple explanation: It's like a game where you unlock a door, and behind that door is another door.

Real-life: If you have a phone, then if it has battery, you can call.

School: If you pass maths, then if you pass science, you get a prize.

Home: If it's your birthday, then if we have cake, we eat cake.

Nigerian: If you have transport money, then if the bus is available, go to the market.

Illustration:

   (if (> age 12)
       (if (equal? name "Ade")
           "you are Ade and older"
           "you are older but not Ade")
       "you are young")
  
๐Ÿ“Œ Mini summary: Nesting lets us make deeper decisions.

Lesson 10 ยท Building a Decision Tree โ€“ Flowcharts

A decision tree is a picture of all the possible choices and their outcomes. It helps us plan our program.

Example: A chatbot that greets people based on the time of day.

   Start
     |
     V
   Ask: what is the hour?
     |
     V
   +---------+---------+---------+
   |         |         |         |
  Morning  Afternoon  Evening  Night
   |         |         |         |
   V         V         V         V
  "Good      "Good     "Good     "Good
   morning"   afternoon" evening"  night"
  
๐Ÿ“Œ Mini summary: Decision trees help us plan complex decisions.

Lesson 11 ยท Boolean Functions โ€“ Returning True/False

Definition: A function that returns #t or #f is called a boolean function. We use them to check conditions.

Why important: They help us build reusable tests.

Simple explanation: It's like a yes/no question that we can ask many times.

Real-life: A function that checks if a number is even: (even? 4) โ†’ #t.

School: A function that checks if a student passed: (pass? score) โ†’ #t if score โ‰ฅ 50.

Home: A function that checks if the door is locked.

Nigerian: A function that checks if someone is over 18 to vote.

Illustration:

   (define (even? n) (= 0 (remainder n 2)))
   (even? 4)  โ†’ #t
   (even? 5)  โ†’ #f
  
๐Ÿ“Œ Mini summary: Boolean functions are tests that return #t or #f.

Lesson 12 ยท Putting It All Together โ€“ A Smart Decision Program

We now build a program that asks for your age and tells you if you can drive, vote, or just play.

   (define (age-check age)
     (cond
       [(< age 12) "You are too young to drive or vote. Play games!"]
       [(< age 18) "You can't vote yet, but maybe you can learn to drive."]
       [(< age 60) "You can drive and vote!"]
       [else "You are a wise elder. Rest and enjoy."]))
   (age-check 10)  โ†’ "You are too young..."
   (age-check 20)  โ†’ "You can drive and vote!"
  

This is a real AI-like program that gives different answers based on your age. You are now building intelligence!

๐Ÿ“Œ Mini summary: We combined many ideas to build a smart age advisor.

๐Ÿ“– Key Vocabulary

  • True (#t): Correct, yes.
  • False (#f): Incorrect, no.
  • Condition: A question that is either true or false.
  • If statement: A command that runs code based on a condition.
  • Cond: A way to check many conditions.
  • AND: True only if everything is true.
  • OR: True if at least one is true.
  • NOT: Flips true to false and false to true.
  • Nesting: Putting one decision inside another.
  • Boolean: A value that is either true or false.

๐Ÿง  Important Concepts

  • Truth values: Everything in logic is either #t or #f.
  • Short-circuiting: In AND, if the first is false, Racket doesn't check the rest.
  • Decision trees: Visual way to plan logic.
  • Reusability: Boolean functions can be reused many times.

๐Ÿ“˜ Step-by-Step Explanations

How to write a decision program:

  1. Identify what decision you need (e.g., can I go out?).
  2. List all the conditions (e.g., is it sunny? is it free?).
  3. Write the conditions using >, <, =, equal? etc.
  4. Use if or cond to choose between actions.
  5. Test with different inputs.

๐ŸŒ Real-life Examples

  • ATM: if you enter correct PIN, give money; else, block card.
  • Spam filter: if email contains certain words, mark as spam.
  • Games: if player reaches score 100, you win.

๐Ÿ‡ณ๐Ÿ‡ฌ Nigerian Examples

  • Bank transfer: if you have enough balance, send money; else, show error.
  • Traffic light at a busy junction: if red, stop; if green, go.
  • Smart irrigation: if soil is dry, turn on water; else, keep off.

๐ŸŽˆ Fun Examples

  • If your pet is hungry, feed it; else, play with it.
  • If your friend says "hello", reply "hello"; if "bye", say "bye".
  • If a number is even, say "even"; else, say "odd".

๐Ÿ  Everyday Examples

  • Waking up: if alarm rings, get up; else, sleep.
  • Cooking: if water is boiling, add pasta; else, wait.
  • Homework: if it's finished, play; else, continue.

๐Ÿง‘โ€๐Ÿซ Teacher Notes

Emphasize that logic is everywhere. Use physical examples: "If I clap, you sit down. Else, stand up." Use cards with #t and #f. Let students write small programs with if and cond. Encourage them to test different inputs.

๐Ÿ‘ช Parent Tips

Ask your child to explain the difference between AND and OR. Play the "if game" at home: "If you clean your room, then you can have a snack." This builds logical thinking. Celebrate when they get the logic right!

๐ŸŒŸ Interesting Facts

  • Boolean logic is named after George Boole, a mathematician.
  • Every modern computer uses boolean logic at its core.
  • AI uses logic to solve complex problems like disease diagnosis.

๐Ÿค” Did You Know?

Racket's cond is so flexible that you can write entire programs using only cond and recursion. Many AI systems are built using these same ideas!

๐Ÿงพ Remember This

  • #t means true, #f means false.
  • Comparisons return #t or #f.
  • if chooses between two paths.
  • cond handles many paths.
  • AND, OR, NOT combine conditions.

โš ๏ธ Common Mistakes

  • Using = to compare strings โ€“ use equal?.
  • Forgetting that and and or are operators, not keywords.
  • Not using else in cond โ€“ your program might return nothing.
  • Nesting too deeply โ€“ keep it simple.

โœ… Best Practices

  • Always make your conditions clear.
  • Use cond for more than 3 options.
  • Write tiny functions that test one thing.
  • Test both true and false cases.

๐Ÿ“Š ASCII Illustrations

   Decision Flow:
   +---------+     +--------+     +--------+
   | Condition | --> | #t?   | --> | Action1 |
   +---------+     +--------+     +--------+
                     |
                     V
                   +--------+     +--------+
                   | #f?    | --> | Action2 |
                   +--------+     +--------+
  
   AND logic:
   Input1 ---+
              AND ---> Output
   Input2 ---+
  

๐Ÿ“‹ Comparison Table

OperatorMeaningExampleResult
>greater than(> 10 5)#t
<less than(< 2 5)#t
=equal (numbers)(= 3 3)#t
equal?equal (any type)(equal? "hi" "hi")#t
andboth true(and #t #t)#t
orat least one true(or #f #t)#t
notflips(not #t)#f

๐Ÿ“Œ End-of-Module Summary

Amazing! You've completed Module Two. You now know how to make your programs decide things. You learned about true/false, comparison operators, if, cond, and logic combiners. You even built a smart age advisor. You are thinking like a real programmer. These decision-making skills are the heart of artificial intelligence. Every AI system, from self-driving cars to smart assistants, uses these same logical building blocks.

โ“ Frequently Asked Questions (10)

  1. What is the difference between if and cond? โ€” if is for two choices; cond is for many.
  2. What does #t mean? โ€” true, yes.
  3. What does #f mean? โ€” false, no.
  4. Can I compare strings with =? โ€” No, use equal? or string=?.
  5. What is else? โ€” It catches any remaining cases in cond.
  6. What does and do? โ€” It's true only if all parts are true.
  7. What does or do? โ€” It's true if at least one part is true.
  8. What does not do? โ€” It flips true to false and false to true.
  9. Can I put if inside if? โ€” Yes, that's nesting.
  10. Why is logic important for AI? โ€” AI uses logic to make decisions and solve problems.

๐Ÿ“ Review Questions (15)

  1. What is #t in Racket?
  2. What operator checks if two numbers are equal?
  3. How do you check if two strings are equal?
  4. What does (if (> 5 3) "yes" "no") return?
  5. Write a cond that gives "A" for 90+, "B" for 80+, else "C".
  6. What does (and #t #f) return?
  7. What does (or #f #f) return?
  8. What does (not #f) return?
  9. What is nesting?
  10. Why do we use else in cond?
  11. Write a function that returns #t if a number is positive.
  12. What is a boolean function?
  13. How do you compare 5 and 7 to see if 5 is less?
  14. What is a decision tree?
  15. Give an everyday example of an if statement.

โœ๏ธ Fill-in-the-Blank

  1. #t stands for ______.
  2. The operator ______ checks if two numbers are equal.
  3. ______ is used to check many conditions.
  4. ______ flips true to false.
  5. We use ______ to combine two conditions where both must be true.

โœ… True or False

  1. #f means true. (False)
  2. You can use = to compare strings. (False)
  3. Cond can handle more than two choices. (True)
  4. AND returns true if at least one is true. (False โ€“ that's OR)
  5. Nesting is putting one if inside another. (True)

๐Ÿ”˜ Multiple Choice (15)

  1. What does (> 8 5) return? A) #t B) #f C) 8 D) 5 โ†’ A
  2. What does (= 5 5) return? A) #t B) #f C) 5 D) error โ†’ A
  3. What does (equal? "cat" "cat") return? A) #t B) #f C) "cat" D) error โ†’ A
  4. Which statement has two choices? A) cond B) if C) and D) or โ†’ B
  5. What does (and #t #f) return? A) #t B) #f C) #t/#f D) error โ†’ B
  6. What does (or #f #t) return? A) #t B) #f C) #t/#f D) error โ†’ A
  7. What does (not #t) return? A) #t B) #f C) #f/#t D) error โ†’ B
  8. Which is used for many choices? A) if B) cond C) and D) not โ†’ B
  9. What is #t? A) true B) false C) number D) string โ†’ A
  10. What does (string=? "Ade" "Ade") return? A) #t B) #f C) "Ade" D) error โ†’ A
  11. What is a boolean function? A) returns #t or #f B) adds numbers C) prints text D) makes lists โ†’ A
  12. What is nesting? A) putting if inside if B) using cond C) using and D) using or โ†’ A
  13. What does (if (< 3 5) "small" "big") return? A) small B) big C) 3 D) 5 โ†’ A
  14. Which is NOT a comparison operator? A) > B) < C) = D) + โ†’ D
  15. What does else do in cond? A) catches other cases B) ends program C) starts loop D) adds numbers โ†’ A

๐Ÿ”— Matching

TermMatch
1. #tA. false
2. #fB. true
3. condC. many choices
4. ifD. two choices

Answers: 1-B, 2-A, 3-C, 4-D

๐Ÿ“ Short Answer

  1. Explain the difference between AND and OR.
  2. Why do we need logic in programming?
  3. Write a cond statement that tells if a number is positive, negative, or zero.

๐Ÿ“– Scenario-based Exercises

Scenario: You are building a smart trash can that opens when a person approaches, but only if it's not raining. How would you write the logic?

Answer: (if (and person-approaches (not raining)) "open" "stay closed")

๐Ÿ‘ฅ Group Activity

Each group writes a "fortune teller" program. It asks for your age and gives a fortune: if under 10, "you will be a pilot"; if 10-15, "you will be a scientist"; else, "you will be a leader". Use cond.

๐Ÿง‘โ€๐Ÿ’ป Individual Activity

Write a function called can-vote? that takes age and returns #t if age โ‰ฅ 18, else #f.

๐Ÿ’ฌ Classroom Discussion Questions

  • Can you think of a decision that requires AND? OR? NOT?
  • Why is it important to test both true and false cases?
  • How does logic help AI make smarter choices?

๐Ÿ› ๏ธ Mini Project

Create a "traffic light" program. Use cond with a color input: "red" โ†’ stop, "yellow" โ†’ wait, "green" โ†’ go, else โ†’ "invalid".

๐Ÿ“‹ Practical Assignment

Write a program that takes a student's score and outputs: "Excellent" for 90+, "Good" for 70-89, "OK" for 50-69, and "Needs Improvement" for below 50.

๐Ÿ† Challenge Exercise

Write a function that takes three numbers and returns the largest. Use cond and comparisons.

๐Ÿ”‘ Quiz Answers

Multiple choice answers: 1-A, 2-A, 3-A, 4-B, 5-B, 6-A, 7-B, 8-B, 9-A, 10-A, 11-A, 12-A, 13-A, 14-D, 15-A.

๐ŸŽ Key Takeaways

  • Logic is the brain of AI.
  • True/false drives every decision.
  • if, cond, and, or, not are your logic tools.
  • Practice building small decision programs.
  • You are now ready for more complex AI!

๐Ÿš€ Preparation for the Next Module

In Module Three, we will learn about repetition โ€“ how to make the computer repeat actions using loops and recursion. We'll build programs that can handle lots of data and do tasks over and over. Practice your logic skills, and get ready to make your programs even more powerful!


๐ŸŽ‰ You've completed Module Two ยท Keep up the great work, young AI specialist! ๐ŸŽ‰

๐Ÿ† Get Certified

๐Ÿ”’

Earn this certificate

Every lesson is already free to read. Sign up, finish the lessons, pass the exam (โ‚ฆ2,000 or 20,000 coins), and get a verified certificate with your name on it.

๐ŸŽ“ Sign Up to Get Certified
๐Ÿ› ๏ธ Practice Tools
Hands-on simulators & labs - subscription required.
โ†’
๐ŸŽฏ Internship Tasks
Real-world tasks to build your portfolio - unlock them with a subscription.
โ†’