Logic and Conditionals
Boolean Review
Comparators
Boolean Operators
If Structures
Booleans
Comparators
Turing Comparators
Symbol | Meaning | Examples |
< | Less than | 5 < 7 → true, 9 < -1 → false |
> | Greater than | 3 > 1 → true, 1 > 3 → false |
<= | Less than or equals | 5 <= 6 → true, 6 >= → false |
>= | Greater than or equals | 9 >= 7 → true, 7 >= 9 → false |
= | Equals | ‘ a ’= ‘a’ → true, 2 = 0 → false |
not= | Not equals | 8 not= 2 → true 9 not= 9 → false |
Notes
Practice
1. 5 < 5 | | 7. 5 < ‘a’ | |
2. 5 > -7 | | 8. -9 > -7 | |
3. 16 <= 9 | | 9. 80 <= 80 | |
4. 7 >= 5 | | 10. 7 >= 5 | |
5. ‘O’ = “OwO” | | 11. 9 = 9.0 | |
6. 5 not= 5 | | 12. “a” not= 5 | |
false
false
false
false
false
false
true
true
true
true
error
error
Boolean Operators
Examples
Practice
5>3 and 6>7 false
5>3 or 6>7 true
not 5>3 and 6>7 false
not (5>3 and 6>7) true
Not (5>3 and 6>7) is the same as not 5>3 or not 6>7
If Structures
Syntax
if condition1 then
put “condition1 is true.”
elsif condition2 then
put “condition1 isn’t true, but condition2 is.”
else
put “Neither of the conditions are true.”
end if
Using Comparators
if distance > 1000 then
put “No, kids, we’re not there yet.”
elsif distance > 50 then
put “Be quiet! We’re, like, almost there.”
else
put “Alright, we’re here! You happy now?”
end if
Using Operators
if myTrophies > 3000 and yourTrophies > 3000 then
put “I guess we’re both Clash pros.”
elsif myTrophies > 3000 or yourTrophies > 3000 then
put “Well, at least one of us is good at this...”
else
put “I’m sure we’ll grind our way up eventually!”
end if
Practice Problem #1!
Practice Problem #1!
Practice Problem #2!
Write a program checking if a user is in high school.
Practice Problem #2!
var age : int
put “What’s your age? ” ..
get age
if 14 <= age and age <= 18 then
put “You’re in high school.”
else
put “You’re not in high school.”
end if
Practice Problem #3!
Modify your previous problem.
Practice Problem #3!
General Tips
Wrapping Up
Final Challenge - Clue