Day 41
Mon 10/26
2.3 if statements (day 1 of 2)
1 of 9 - Story
Skips the Whole Party
Why is a one-way if a terrible host? When the condition is false, it skips the whole party.
2 of 9 - Vocab
- selection
- A statement that chooses whether some code runs, based on a condition. The if and the if-else are Java's selection statements.
- one-way if
- An if with no else. Its body runs only when the condition is true. When the condition is false, the body is skipped and the program continues after the closing brace.
- condition
- The boolean expression inside the parentheses of an if, like score >= 70. It has to be a boolean, so if (x = 5) does not compile.
- block
- The statements between { and } that belong to an if. Without braces, only the very next statement belongs to the if, which is a classic trap.
3 of 9 - Scenario
Nina is coding the ticket booth for the school's haunted hallway on Halloween night. She stores int tickets = 4; and int total = 20; and then writes three separate if statements in a row: if (tickets > 3) { total = total - 5; } if (total > 20) { total = total + 100; } if (tickets == 4) { total = total * 2; } Her friend Leo says total will end at 15, because once one if is true the others are skipped. Nina decides to trace each statement in order and write down what total holds after every one.
What does total hold when all three statements have been checked?
- Correct30
The first condition 4 > 3 is true, so total becomes 15. The second condition 15 > 20 is false, so it is skipped. The third condition 4 == 4 is true, so total becomes 15 * 2, which is 30.
Ties to today: Each one-way if is checked on its own, using the values at that moment. That is the heart of Topic 2.3.
- Wrong15
That would be true if the other ifs were skipped after the first true one, but separate ifs are each checked on their own. The third condition is true as well.
- Wrong230
The second if checks total after the discount, when total is already 15. Since 15 > 20 is false, the + 100 never happens.
- Wrong40
The third if multiplies the current total, which is 15 after the discount, so it gives 30 and not 40.
4 of 9 - Quiz
int x = 8; if (x > 10) { x = 0; } What does x hold afterward?
- Correct8
- Wrong0
- Wrong10
- WrongThe code does not compile
What happens in a one-way if when its condition is false?
- CorrectThe body is skipped and the program continues after it
- WrongThe program stops with an error
- WrongThe body runs anyway
- WrongThe else block runs
Which of these lines does NOT compile?
- Correctif (x = 3) { x++; }
- Wrongif (x == 3) { x++; }
- Wrongif (x != 3) { x++; }
- Wrongif (x > 3) { x++; }
int n = 5; if (n > 2) n = n + 1; n = n * 10; What does n hold afterward?
- Correct60
- Wrong6
- Wrong50
- Wrong5
Fill in: the boolean expression inside the parentheses of an if is called the ___.
Accepted answers: condition, the condition
5 of 9 - Reflection
In your own words, explain what a one-way if does when its condition is false. Then write one real decision from your day as an if statement in plain English.
6 of 9 - Instruction
Today's Task: Selection Lab - Day 1
7 of 9 - Instruction
Weekly Task (due Friday): FRQ Challenge: Haunted Hallway, Read and Part (a)
8 of 9 - Instruction
Bonus Practice: CodingBat: Logic-1
9 of 9 - Instruction