Boolean If:

3.5 & 3.6 Boolean Expressions & Conditionals

Relational Operators

  • A boolean value is either true or false
  • Typically yes or no questions like “do I have a dog?”
  • So when you write an expression to evaluate, you need to use different types of relational operators determining on the questions that we’re trying to ask
    • Ex: when trying to test to see whether or not two values or two variables are equal, then you use the equal sign to check whether or not two values or two variables contain the same value
      • a = b [equal to] numStudents = 30
      • This checks whether the value is also equal to 30
  • You can also use the not equal sign to evaluate whether or not two values are not equal to each other
    • a ≠ b [not equal to] count ≠ 10
  • We can also use the greater than or less than symbol to check whether or not something is greater than or less than then the other
    • a > b [greater than] grade > 70
  • ou can also use the greater/less than or equal to
    • a ≥ b [greater than or equal to] numPets ≥ 0
num1 = 5  
num2 = 7  

are_equal = num1 == num2

print(are_equal)  # This will print False in this example
False
num1 = 5  
num2 = 7  

are_not_equal = num1 != num2

print(are_not_equal)  # This will print True in this example
True
num1 = 5  
num2 = 7  

is_less_than = num1 < num2

print(is_less_than)  # This will print True in this example
True
num1 = 5  
num2 = 7  

is_greater_than = num1 > num2

print(is_greater_than)  # This will print False in this example
False

Logical Operators

  • NOT condition: flips what is currently stored in the variable without impacting the variable/condition itself
  • AND condition: checks both conditions to produce one single Boolean true or false
  • OR condition: checks whether or not one of the conditions is true or false
  • These logical operators can be used with boolean expressions to produce a single Boolean value
# not condition
isRaining = False
result = not isRaining
print(result)
True
# and conditions
grade = 85
result = grade > 70 and grade < 100
print(result)
grade = 59
result = grade > 70 and grade < 100
print(result)
# or conditions
score = 175
highScore = 150
lives = 2
result = (score > highScore) or (lives > 3)
print(result)
score = 175
highScore = 150
lives = 2
result = (score < highScore) or (lives > 3)
print(result)
True
False

Popcorn Hack

  • Create a code segment with an OR operator that is True. With the same score, change the conditions and create another expression that prints False instead of True.
score = 800
highScore = 270
lives = 2
result = (score > highScore) or (lives > 3)
print(result)
score = 800
highScore = 270
lives = 2
result =(score< highScore) or (lives > 3)
print(result)
True
False

Relational & Logical Operators

  • Boolean values are either true or false
  • relational operators: =,≠, >, <, ≥, and ≤
# call back to relational operators
a = b
a  b
a > b
a < b
a  b
a  b

These are used to test the relationship between two variables, expressions, or values. A comparison between a relational operator evaluates to a Boolean value.

# call back to logical operators
result = not isRaining # not condition

result = (score > highScore) or (lives > 3) # or condition

result = grade > 70 and grade < 100 # and condition

Different Expressions, Same Results

  • age ≥ 16
  • age > 16 or age = 16
  • NOT age < 16

Example

num = 7
isEven = num % 2 == 0
range = (num > 5) and (not num > 10)
display (isEven or range)

True
# practice
name = "Hannah"
age = 17
display ((not (name == "Hannah")) or (age < 20))
True

Popcorn Hack

Create a Boolean expression using your favHobby as a variable and the number of classes you are taking as the second variable.

  • Must have:
  • favHobby and number of classes –> 2 variables
  • display expression; NOT and OR logical operators
  • one false expression and one true expression = overall true
# start with
favHobby = "sports"
numClasses = 4
display((not (favHobby != "sports")) or (numClasses < 5))
True

learning objective: write algorithms that uses selection without using a programming language

vocab

selection: determines which part of an algorithm are executed based on condition being true or false algorithm: a finite set of instructions that accomplish a specific task

ex determine if a number is in range

  • get number from user
  • is number greater than - and less than 10
  • if yes display number
  • if no display goodbye

participation hack:

give me different ways you would determine if a kid needs to go to tutoring

import random

student = ("failing", "focused")
randStu = random.choice(student)
if randStu == "failing":
    print("The student needs tutoring because he is", randStu)
    
else:
    print("the student doesn't need tutoring")
The student needs tutoring because he is failing

writing conditionals

format

if (condition) :

  • if the condition evaluates to true then the block of statements occurs
  • if not then no action is taken or if (condition) : else:
  • if the condition is true then first block is executed if false then the second black or else statement is executed

Example – multiple of 3?

number = 9
if number % 3 == 0 :
    print ("multiple of 3")
else:
    print ("not multiple of 3")
multiple of 3

Popcorn Hack

calculate the sum of two numbers -> if the sum is greater than 100 display 100, otherwise display the sum.

help you guys get started: num 1 = num 2 = sum = num 1 + num 2 if (): else :

import random
num1 = random.randint (0, 100)
num2 = random.randint (0,100)
sum = num1 + num2
if (sum > 100):
    print("the sum is greater than 100, it is: " + str(sum))

else:
    print("the sum is less than 100, it is: " + str(sum))

the sum is greater than 100, it is: 123

We can do conditional statements using if blocks that just choose or do one particular outcome based off of a condition

Or you can do if-else blocks to choose from two different outcomes

IF (condition) { } ELSE { } The else only activates if the “if condition” is FALSE

Example 1: if-elif-else block


y = 7
if y > 10:
    print("y is greater than 10")
elif y == 10:
    print("y is equal to 10")
else:
    print("y is less than 10")

y is less than 10
  • conditional statements, or “if-statements,” affect the sequential flow of control by executing different statements based on the value of a boolean expression
  • The if/else statement executes a block of code if a specified condition is true. If the condition is false, another block of code can be executed.
  • we use an if-elif-else block to check the value of the variable y. It first checks if y is greater than 10, then if it’s equal to 10, and finally, if neither of these conditions is met, it executes the code inside the else block.

Homework:

Hack 1

Write two Boolean expressions to check if the average grades of student1, student2, and student3 is at least 85. Write one expression that is false and one that is true.

Hack 2

You can’t go outside if the temperature is less than 20 AND the weather is stormy. Write the Boolean expression for the scenario. Screenshot your work and put it on a Google document. Share the link with Ellie Rozenkrants on Slack by Oct 12 11:59 PM.

#true statement
student1 = 100
student2 = 73
student3 = 89

averageStu = (student1 + student2 + student3)/3
if averageStu >= 85:
    display(averageStu >= 85)
    print("Their average is at least 85, " + str(averageStu) + "%")
else:
    display(averageStu >= 85)
    print("Their average is below 85, " + str(averageStu) + "%")
    
#false statement   
student1 = 100
student2 = 73
student3 = 60

averageStu = (student1 + student2 + student3)/3
if averageStu >= 85:
    display(averageStu >= 85)
    print("Their average is at least 85, " + str(averageStu) + "%")
else:
    display(averageStu >= 85)
    print("Their average is below 85, " + str(averageStu) + "%")
True


Their average is at least 85, 87.33333333333333%



False


Their average is below 85, 77.66666666666667%
import random
weather = ["stormy", "sunny", "windy", "foggy"]
randWeather = random.choice(weather)

temp = [18, 30, 70, 108]
randtemp = random.choice(temp)

if randtemp < 20 and randWeather == "stormy":
    print("You can't go outside bro")
elif randWeather =="stormy" and randtemp > 20:
    print("be careful outside. it is " + randWeather)
else:
    print("have fun in this weather:" + randWeather)
    print("the Temperature is: " + str(randtemp))
You can't go outside bro

Reflection:
Boolean If Statements:

  • Decision Making: Boolean if statements are fundamental for decision-making in computer programs.

  • Conditional Execution: I learned how to use if statements to execute specific code blocks only when certain conditions are met.

  • Logical Operators: I explored logical operators like “AND” and “OR” to create complex conditions for decision-making.

  • Branching: If statements allow programs to branch into different paths based on conditions, which is essential for dynamic behavior.

  • Nested Ifs: I discovered how to use nested if statements to handle multiple conditions and create more intricate decision structures.

  • Flow Control: Boolean if statements are critical for controlling the flow of a program, enabling it to respond dynamically to changing situations.

  • Error Handling: I realized that if statements can be used to handle errors and exceptions by checking conditions and responding accordingly.

  • Simplicity and Readability: Writing clear and concise if statements is essential for program readability and maintenance.

  • Debugging: I learned the importance of debugging if statements to ensure they work as intended and avoid logical errors.

  • Real-World Applications: Boolean if statements are used extensively in real-world applications to create responsive and adaptive software.

  • In summary, mastering Boolean if statements in computer science is about understanding how to make decisions, control program flow, and create responsive software that can adapt to changing conditions and requirements. These fundamental skills are essential for any programmer.