Video 3.5-7 Hacks
Unit 3 Section 5
Learning Objectives
- Write expressions with logical operators
- Evaluate expressions that use logical operators
Vocab
Boolean binary variable with only two possible values, "true" or "false"
Relational Operators
There are multiple operators which are used to evaluate 2 variables. In pseudocode the relational operators are =, >, <, ≠, ≥, and ≤. Other languages use different formats to represent these operators. For example, Python uses != instead of ≠. These operators help determine the boolean value of a statement.
Logical Operators
Logical operators allow for boolean values to be evaluated. Pseudocode uses the logical operators NOT, AND, and OR. Javascript uses the same logic, but uses different ways to represent the operators: &&(and), | |(or), |(not).
Operators and booleans can be very useful, allowing a program to create an algorithm based on user inputs items stored in list. For example, operators can be used to perform multiple operations on lists.
Unit 3 Section 6
Learning Objectives
- Write conditional statements
- Determine the result of conditional statements
Vocab
Algorithm: Finite set of instructions that accomplish a specific task
Conditional Statements
- Conditional statements allow the code to act based on multiple conditions, such as user input or stored data.
- We can use conditionals in algorithms in order to receive different outputs from a set of code, and help achieve the general purpose of the algorithm.
Categories
Two broad types of conditional statements are if statements and if-else statements
- if statement: will go through certain statements if the if expression is true
- if-else statement: will go through a statement no matter what, but the statement which is run depends on the if expression is true or false
Flow Charts
- We can use flowcharts to help organize the process of conditional statements.
- This allows us to diagram the work and have an image of what the process will look like
We can use this to write pseudocode.
S <-- 12 IF (S MOD 2 = 0) { DISPLAY (S) } ELSE { DISPLAY ("This number is not even.") }
Converting this to a sequence of python code is very easy thanks to having an easy plan to follow.
Unit 3 Section 7
Learning Objectives
Write nested conditional statements Determine the result of nested conditional statement
Vocab
Nested conditional: A conditional inside of a conditional
Nested Conditionals
Nested conditionals are often used in algorithms in order to print specific data or run certain tasks.
We can use conditionals inside conditionals to create further conditions in algorithms.
We can use flowcharts to help display an easy to understand diagram of what we want a code segment to do.
Value 1 | Value 2 | Result |
---|---|---|
1 | 0 | 1 |
1 | 0 | 0 |
1 | 1 | 0 |
1 | 0 | 0 |
Value 1 | Value 2 | Result |
---|---|---|
1 | 1 | 0 |
0 | 0 | 1 |
0 | 1 | 1 |
0 | 1 | 0 |
Not | Value | Result |
---|---|---|
Not | 1 | 0 |
Not | 0 | 1 |
# Practice with these statements
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
print(x == z)
3.6 Hacks
AP Prep
1. What is displayed by this code?
- result <-- 75
- IF result < 80 { DISPLAY("Please schedule a retake.") }
- ELSE { DISPLAY("Nice job!") }
- Nice job!
2. How is an if statement different from an if-else statement.
- 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.
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-else statement
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
Using Python
animals = ["lion", "tiger", "wildebeest", "shark", "jellyfish", "blobfish", "raven"]
for i in animals:
if i == "shark": # What boolean value does this statement cause? 3
print("Fun Fact: The smallest shark is the dwarf lantern shark, and it is small enough to hold in your hand!")
if i == "lion": # What boolean value does this statement cause? 3
print("It 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
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
InfoDb = []
# Append to List a Dictionary
InfoDb.append({
"foodName": "Chicken Alfredo",
"meat": "Chicken",
"timeToPrepare": "60 minutes",
})
# Append to List a 2nd Dictionary of key/values
InfoDb.append({
"foodName": "Cheese Quesadilla",
"meat": "None",
"timeToPrepare": "10 minutes",
})
# Append to List a 3rd Dictionary of key/values
InfoDb.append({
"foodName": "Beef Wellington",
"meat": "Beef",
"timeToPrepare": "150 minutes",
})
# given and index this will print InfoDb content
def print_data(d_rec):
print(d_rec["foodName"], "Time to Prepare:", d_rec["timeToPrepare"]) # using comma puts space between values
print("Meat:", d_rec["meat"])
# for loop iterates on length of InfoDb
def for_loop():
print("")
for record in InfoDb:
print_data(record)
for_loop()
Exercise 2
Make a flowchart(here is one we used) and write pseudocode for the following scenario.
- Mr. Yeung would like to grade live reviews.
- He wants to see if each student has at least 2 issues on their project. If they don't they receive a score of 2.0.
- If they have at least 2 issues, check that they have completed at least 5 of their scrumboard tasks.
- If they have completed 5 scrumboard tasks, give the student a 2.7. If they have not completed 5 scrumboard tasks, give them a score of 2.5. If they have completed more than 5 tasks, give them a score of 3.0.
- How much would a student with 3 issues and 1 complete scrumboard task receive?
They would get 3.0.