1 of 22

Logic and Conditionals

Boolean Review

Comparators

Boolean Operators

If Structures

2 of 22

Booleans

  • A basic variable type
  • Stores true or false
  • What we’ll do with them:
    • Create booleans by comparing values
    • Combine booleans into logic statements
    • Make decision-making programs

3 of 22

Comparators

  • Compares two inputs of the same type
  • Math expressions evaluate to a number
    • Eg. 10 + 89 = 99
  • These evaluate to a boolean value
    • Eg. -2 < 5 = true, 4.6 > 9.1 = false

4 of 22

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

5 of 22

Notes

  • You can compare ints and reals
  • You can compare chars and Strings
    • Their ASCII values are used for comparisons
  • Booleans can only be compared for equality
  • = checks for equality, := assigns a value
    • Don’t use = to assign values
  • Be careful when comparing reals (rounding errors)

6 of 22

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

7 of 22

Boolean Operators

  • Like math operators, but for boolean values
    • They’ll combine expressions into a value
  • and shows if both inputs are true
  • or shows if one of its are true
  • not gives the opposite of its input

8 of 22

Examples

  • true and true = true, true and false = false
  • true or false = true, false or false = false
  • not true = false, not false = true

9 of 22

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

10 of 22

If Structures

  • Tells computer to execute or skip code
    • The behaviour depends on the condition given
  • Eg. If battery is over 10%, keep playing
  • We add on extra cases if the condition isn’t true
    • Else, turn off the phone and study

11 of 22

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

12 of 22

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

13 of 22

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

14 of 22

Practice Problem #1!

  1. Create a string variable called password
  2. Ask the user for the password
  3. If it matches, output “CORRECT”
  4. Otherwise, output “INCORRECT”

15 of 22

Practice Problem #1!

16 of 22

Practice Problem #2!

Write a program checking if a user is in high school.

  1. Ask the user their age
  2. If they’re between ages 14 and 18 inclusive…
  3. Output “You’re in high school.”
  4. Otherwise, output “You’re not in high school.”

17 of 22

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

18 of 22

Practice Problem #3!

Modify your previous problem.

  1. Ask the user their age
  2. If they’re below 0, praise their guru powers
  3. If they’re between 14 and 17 inclusive, say they’re in highschool
  4. Otherwise, say they’re not in highschool

19 of 22

Practice Problem #3!

20 of 22

General Tips

  • Try to make your logic readable
    • Should be understandable at a glance
  • Remember that < and > aren’t inclusive
  • If a variable is a boolean, don’t do variable = true
    • Will just give the variable’s value
  • CTRL-ENTER auto completes if structures, procs, and loops

21 of 22

Wrapping Up

  • Booleans can be true or false
    • Comparators create booleans
    • Operators help us make logical statements
  • If structures make the code react to conditions
  • You can have as many elsif‘s as you want

22 of 22

Final Challenge - Clue

  • Create variables for info on a murder
    • Eg. Location, Killer’s Height, etc.
  • Interrogate the user for their alibi
    • Where were you on April 25th?
    • How tall are you?
  • Output if they’re the killer or not