Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.
This else if chain versus separate if statements worksheet is part of the ACARA Digital Technologies Years 7–8 — Programming with Control Structures and Functions (JavaScript) journey. Print it as a booklet?
Shade in the A or B circle for each question.
Draw a line connecting each item on the left to its match on the right.
| Left column | Right 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'. |
Complete each line of code by filling in the missing parts.
Shade in the letter of the line that contains an error.
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.if (age < 13) { console.log("Child"); }
else if (age < 20) { console.log("Teen"); }let score = 95;
if (score >= 50) { console.log("Pass"); }
if (score >= 90) { console.log("Top"); }if (n > 0) { console.log("Positive"); }
else if (n < 0) { console.log("Negative"); }
else { console.log("Zero"); }let x = 7, count = 0;
if (x > 0) { count++; }
if (x > 5) { count++; }if (mark >= 90) { console.log("A"); }
else if (mark >= 70) { console.log("B"); }
else { console.log("C"); }