else if chain versus separate if statements

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

else if chain versus separate if statements

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. In an if / else if / else chain, how many branches can run?
A At most one branch
B Every branch whose condition is true
2. let score = 85; Two separate statements: if (score > 50) { log('Pass') } and if (score > 80) { log('High') }. What is logged?
A Only Pass
B Pass and High
3. let score = 85; if (score > 80) { log('High') } else if (score > 50) { log('Pass') }. What is logged?
A Only High
B High and Pass
4. Which code can run two blocks in a single pass?
A Two separate if statements
B One if / else if chain
5. You want to give exactly one grade (A, B or C) for a mark. Which should you use?
A Separate if statements
B An if / else if chain

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1if (score >= 50) { grade = 'Pass'; } if (score >= 80) { grade = 'High'; } AAn else if chain: it stops at the first true condition, so when score is 85 only 'High' runs and the second test is skipped.
2if (score >= 80) { grade = 'High'; } else if (score >= 50) { grade = 'Pass'; } Bif (temp > 30) { console.log('Hot'); } else if (temp > 20) { console.log('Warm'); } else { console.log('Cool'); }
3Use separate if statements when more than one thing can be true at once, such as adding a bonus for each rule met. CAn else if is used for two independent checks. If isRaining is true, isSunny is never checked, so the hat is skipped.
4Use an else if chain when only one outcome should happen, such as picking a single weather message. DSeparate ifs are used where only one grade should apply. A mark of 95 passes all three tests, so grade ends up as 'C'.
5if (mark >= 90) { grade = 'A'; } if (mark >= 70) { grade = 'B'; } if (mark >= 50) { grade = 'C'; } Eif (age < 13) { cost -= 2; } if (isMember) { cost -= 1; }
6if (isRaining) { bag = 'umbrella'; } else if (isSunny) { hat = 'cap'; } FTwo separate if statements: both are checked, so when score is 85 both run and grade ends up as 'High'.

Definitions

else if

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
if (age < 13) { console.log("Child"); } ____ if (age < 20) { console.log("Teen"); }
2.
let score = 95; if (score >= 50) { console.log("Pass"); } __ (score >= 90) { console.log("Top"); }
3.
if (n > 0) { console.log("Positive"); } else if (n < 0) { console.log("Negative"); } ____ { console.log("Zero"); }
4.
let x = 7, count = 0; if (x > 0) { count++; } if (x > 5) { _____++; }
5.
if (mark >= 90) { console.log("A"); } _______ (mark >= 70) { console.log("B"); } else { console.log("C"); }

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A let temp = 32;
B else if (temp > 30) {
C console.log("Hot");
D } else if (temp > 20) {
E console.log("Warm");
F }
Question 2
A let n = 8, big = 0, mid = 0;
B if (n > 5) { big++; }
C if (n > 3) { mid++; }
D console.log(big, mid);
Question 3
A let age = 15;
B if (age < 13) {
C console.log("Child");
D } else if age < 20 {
E console.log("Teen");
F }
Question 4
A let n = -2, pos = 0, neg;
B if (n > 0) { pos++; }
C else if (n < 0) { neg++; }
D console.log(pos, neg);
Question 5
A funtion level(n) {
B if (n >= 80) return 3;
C else if (n >= 50) return 2;
D return 1;
E }

Answer Key

else if chain versus separate if statements
Section A: Quick Questions
Q1
A — At most one branch
Q2
B — Pass and High
Q3
A — Only High
Q4
A — Two separate if statements
Q5
B — An if / else if chain
Section B: Match the Pairs
Q1
if (score >= 50) { grade = 'Pass'; } if (score >= 80) { grade = 'High'; } → Two separate if statements: both are checked, so when score is 85 both run and grade ends up as 'High'.
Q2
if (score >= 80) { grade = 'High'; } else if (score >= 50) { grade = 'Pass'; } → An else if chain: it stops at the first true condition, so when score is 85 only 'High' runs and the second test is skipped.
Q3
Use separate if statements when more than one thing can be true at once, such as adding a bonus for each rule met. → if (age < 13) { cost -= 2; } if (isMember) { cost -= 1; }
Q4
Use an else if chain when only one outcome should happen, such as picking a single weather message. → if (temp > 30) { console.log('Hot'); } else if (temp > 20) { console.log('Warm'); } else { console.log('Cool'); }
Q5
if (mark >= 90) { grade = 'A'; } if (mark >= 70) { grade = 'B'; } if (mark >= 50) { grade = 'C'; } → Separate ifs are used where only one grade should apply. A mark of 95 passes all three tests, so grade ends up as 'C'.
Q6
if (isRaining) { bag = 'umbrella'; } else if (isSunny) { hat = 'cap'; } → An else if is used for two independent checks. If isRaining is true, isSunny is never checked, so the hat is skipped.
Section C: Definitions
Q1
else if — In an else if chain, only the first true branch runs and the rest are skipped. Separate if statements are each checked, so more than one can run.
Section D: Fill in the Blanks
Q1
if (age < 13) { console.log("Child"); } else if (age < 20) { console.log("Teen"); }
Q2
let score = 95; if (score >= 50) { console.log("Pass"); } if (score >= 90) { console.log("Top"); }
Q3
if (n > 0) { console.log("Positive"); } else if (n < 0) { console.log("Negative"); } else { console.log("Zero"); }
Q4
let x = 7, count = 0; if (x > 0) { count++; } if (x > 5) { count++; }
Q5
if (mark >= 90) { console.log("A"); } else if (mark >= 70) { console.log("B"); } else { console.log("C"); }
Section E: Spot the Error
Q1
B — An else if must continue an existing if. Starting the chain with else if is a syntax error. The first test should be a plain if (temp > 30) {.
Q2
C — Writing a plain if here makes it a separate check, so both big and mid go up when n is 8. The chain should use else if (n > 3) so that only the first true branch runs.
Q3
D — The condition of an else if must be inside brackets, just like an if. It should be } else if (age < 20) {.
Q4
A — neg is never given a starting value, so it is undefined and neg++ gives NaN. The counter must start at 0: neg = 0. Only the matching branch of the chain runs, so every counter needs a start value.
Q5
A — The keyword is misspelled. A function that holds an else if chain must be declared with function, not funtion.