3.5 notes

Logical operators evaluate boolean values. Pseudocode will use Not , AND, and OR to demonstrate this.

practice questions 3.5

q 1. What boolean value would result be?

a <-- 19 b <-- 28 result <-- a = b

True

False

ANS: False b/c a /= b

q 2. What boolean value would result be?

score <-- 99 average <-- 99 result <-- score ≤ average

True

False

ANS: True b/c score = average

q 3. What boolean value would be displayed?

type <-- "dog" training <-- "yes" DISPLAY((NOT (type = "dog")) AND (training = "no"))

True

False

ANS: FALSE AND FALSE b/c both statements are false

3.6 notes

conditional statements are when the code acts based on different conditions based on user input or stored data.

ex: if statements and if else statements are conditional statements

HINT: flowcharts can help visualize conditional statements

practice questions 3.5

q 1. What value will be displayed

rank <-- "titan" hall <-- 14 IF (rank = "titan") { DISPLAY("You like Clash of Clans a lot.") }

You like Clash of Clans a lot.

14

titan

rank

ANS: Since rank = "titan", the if statement is executed, displaying the code in it. ()

q 2. What portion of the code will run?

class <-- 33 cookies <-- 25 IF (class < cookies) { DISPLAY("Give out cookies today.") } ELSE { DISPLAY("Do not give out cookies today.") }

The if portion

The else portion

ANS: else portion b/c class < cookies

3.7 notes

NESTED CONDITIONAL: conditional statement in a conditional statement

HINT: flowcharts will be helpful to visualize these

3.7 practice questions

q 1. Is this an example of a nested conditional?

space <-- 90 download <-- 20 IF (space > download) { DISPLAY("File can be downloaded.") }

Yes

No

ANS: no b/c theres only one conditional statement

q 2. What does this code display?

price <-- 200 discount <-- 65 IF (price > 100) { IF (discount > 50) { DISPLAY("You can buy this item.") } ELSE { DISPLAY("You cannot purchase this item.") } }

You cannot purchase this item.

50

You can buy this item.

65

ANS: You cannot purchase this item b/c discount < 50

3.5 HACKSSSS

Binary Practice Using psuedocode operators determine if the statements are true or false. The number type will be indicated in parentheses.

1 90(D) = 1000(B)

A. True

B. False

ANS: B

2 10(D) ≠ 0110(B)

A. True

B. False

ANS: B

3 56(D) ≥ 111000(B)

A. True

B. False

ANS: A

4 99(D) < 1110011(B)

A. True

B. False

ANS: B

binary truth tables

AND OPERATOR

Value 1 Value 2 Result
1 1 1
1 0 0
0 1 0
0 0 0

OR Operator

Value 1 Value 2 Result
1 1 1
1 0 1
0 1 1
0 0 0

Not operator

Not Value Result
Not 1 0
Not 0 1

Python Practice

print(20 < 20) # How can you change the operator to print a value of False?

x = 30
y = 20
z = 10
print(x == y + z) # How can this return true by only manipulating the operator?

# Manipulate the variables x, y, and z to make the below statement return true
x = 20
z = 20
print(x == z)
False
True
True

3.6 hacks

AP PREP

q 1. What is displayed by this code?

result <-- 75 IF result < 80 { DISPLAY("Please schedule a retake.") } ELSE { DISPLAY("Nice job!") }

Nice job!

Display

Please schedule a retake.

75

ANS: Please schedule a retake.

q 2. How is an if statement different from an if-else statement.

Extra words.

An if statement will only go through a process if a condition is met. An if-else statement will go through code no matter the conditions.

They are the exact same.

An if statement will go through the entire code segment every single time and the if-else statement is always used in an algorithm, no matter the conditions.

ANS: An if statement will only go through a process if a condition is met. An if-else statement will go through code no matter the conditions. (2) b/c if - else has to go through code because it will do something even if the condition is not what the if statement wants

q 3. What would be most appropriate for this situation? Ben wants to check his bank account. If his car fuel is full, he will go to the bank. Otherwise, he will go home. If he goes to the bank, he will withdraw money only if his balance is above $1000.

If statement

If-else statement

ANS: IF-else statement

q 4. What would be most appropriate for this situation? Luke wants to play basketball. If it is sunny outside he will go to the park to play basketball.

If statement

If-else statement

ANS: If statement cause we don't know what he'll do if it isn't sunny

Using Python

animals = ["lion", "tiger", "wildebeest", "shark", "jellyfish", "blobfish", "raven"]

for i in animals:
    if i == "shark": # What boolean value does this statement cause?
        # it should be true when the for loop gets to shark
        print("Fun Fact: The smallest shark is the dwarf lantern shark, and it is small enough to hold in your hand!")
    if i == "wildebeest" or i == "lion":
            print(i,",this animal lives in the desert")
    else:
        print(i)

# Practice
# Using only one more if statement, alter the code to print out a statement saying if an animal lives in the desert, based on booleans
lion ,this animal lives in the desert
tiger
wildebeest ,this animal lives in the desert
Fun Fact: The smallest shark is the dwarf lantern shark, and it is small enough to hold in your hand!
shark
jellyfish
blobfish
raven

3.7 HACKS

Exercise 1 Create dictionaries for multiple food items, with the listed specifications Chicken Alfredo, Meat: Chicken, Time to Prepare: 60 minutes Cheese Quesadilla, Meat: None, Time to Prepare: 10 minutes Beef Wellington, Meat: Beef, Time to Prepare: 150 minutes Used nested conditionals, determine which meal you can cook, given that a) you have no meat at home, and b) you only have 30 minutes to make the meal

BefWil = {
    "foody":"Beef wellington",
    "meat": "beef",
    "preptime": "150 minutes"
}
ChickenAlfredo = {
    "foody":"ChickenAlfredo",
    "meat": "Chicken",
    "preptime": "60 minutes"
}

Ques = {
    "foody":"cheese quesadilla",
    "meat": "None",
    "preptime": "10 minutes"
}

#function to see if meal meets requirements
def dinner(food):
    if food["meat"] == "None":
        if food["preptime"] <= "30":
            print(food["foody"])

            
dinner(ChickenAlfredo)
dinner(Ques)
dinner(BefWil)
cheese quesadilla

FlowCHART