Nested if statements

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

Nested if statements

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. When is the inner if checked in: if (age >= 13) { if (hasPermission) { ... } }
A Every time the program runs
B Only when age >= 13 is true
2. Which nested if is written correctly?
A if (score > 50) { if (score > 90) { console.log('Top!'); } }
B if (score > 50) { if (score > 90) { console.log('Top!'); }
3. Is the inner if indented again in good style?
A No, it lines up with the outer if
B Yes, it moves in one more level to show it is inside the outer if
4. let x = 3; if (x > 5) { if (x > 1) { console.log('Hi'); } } What is printed?
A Nothing
B Hi
5. Which && condition does the same job as: if (a) { if (b) { ... } }
A if (a && b) { ... }
B if (a || b) { ... }

Match the Pairs

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

Left columnRight column
1if (age >= 13) { if (hasPermission) { console.log("Join"); } } Aif (isLoggedIn && isAdmin) { console.log("Welcome"); }
2if (score > 50 && lives > 0) { BThe inner if (hasPermission) is only checked when age >= 13 is true. It sits inside the outer braces and is indented again.
3Each if has its own braces, and the inner if is indented again inside the outer block Cif (raining) { if (hasUmbrella) { console.log("Walk"); } }
4A nested if is the same as one if joined with && when there is no else DThe inner if is missing its closing brace, so the braces do not match. Each if needs its own { and }.
5if (x > 0) { if (x < 10) { console.log("Small"); } EOne if with a combined condition. Both parts must be true, and no inner if is needed.
6if (age >= 13) { if (hasPermission === true) { console.log("Join"); } } FThe console.log line is not indented again inside the inner if. Its position makes it look like it belongs to the outer if.

Definitions

if (a) { if (b) { } }

Fill in the Blanks

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

1.
if (age >= 13) { __ (hasPass) { console.log("Enter"); } }
2.
if (score >= 50) { if (score __ 90) { console.log("Top"); } }
3.
if (isRaining) { if (hasUmbrella) { console.log("Go"); _ }
4.
if (age >= 13 __ hasPass) { console.log("Enter"); }
5.
if (loggedIn) { if (role ___ "admin") { console.log("Hi"); } }

Spot the Error

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

Question 1
A let age = 16;
B if (age >= 13) {
C if (age < 18) {
D console.log("Teen";
E }
F }
Question 2
A let score = 85;
B if (score => 50) {
C if (score >= 80) {
D console.log("Great");
E }
F }
Question 3
A let temp = 28;
B if (temp > 25) {
C if (temp > 35)
D console.log("Too hot");
E }
F }
Question 4
A let level = 6;
B If (level >= 5) {
C if (level >= 10) {
D console.log("Expert");
E }
F }
Question 5
A let lives == 3;
B if (lives > 0) {
C if (lives === 1) {
D console.log("Last life");
E }
F }

Answer Key

Nested if statements
Section A: Quick Questions
Q1
B — Only when age >= 13 is true
Q2
A — if (score > 50) { if (score > 90) { console.log('Top!'); } }
Q3
B — Yes, it moves in one more level to show it is inside the outer if
Q4
A — Nothing
Q5
A — if (a && b) { ... }
Section B: Match the Pairs
Q1
if (age >= 13) { if (hasPermission) { console.log("Join"); } } → The inner if (hasPermission) is only checked when age >= 13 is true. It sits inside the outer braces and is indented again.
Q2
if (score > 50 && lives > 0) { → One if with a combined condition. Both parts must be true, and no inner if is needed.
Q3
Each if has its own braces, and the inner if is indented again inside the outer block → if (raining) { if (hasUmbrella) { console.log("Walk"); } }
Q4
A nested if is the same as one if joined with && when there is no else → if (isLoggedIn && isAdmin) { console.log("Welcome"); }
Q5
if (x > 0) { if (x < 10) { console.log("Small"); } → The inner if is missing its closing brace, so the braces do not match. Each if needs its own { and }.
Q6
if (age >= 13) { if (hasPermission === true) { console.log("Join"); } } → The console.log line is not indented again inside the inner if. Its position makes it look like it belongs to the outer if.
Section C: Definitions
Q1
if (a) { if (b) { } } — A nested if is an if inside another if's braces. The inner if is only checked when the outer condition is true. Each if has its own braces and the inner one is indented again.
Section D: Fill in the Blanks
Q1
if (age >= 13) { if (hasPass) { console.log("Enter"); } }
Q2
if (score >= 50) { if (score >= 90) { console.log("Top"); } }
Q3
if (isRaining) { if (hasUmbrella) { console.log("Go"); } }
Q4
if (age >= 13 && hasPass) { console.log("Enter"); }
Q5
if (loggedIn) { if (role === "admin") { console.log("Hi"); } }
Section E: Spot the Error
Q1
D — The console.log call is missing its closing bracket. It should be console.log("Teen"); with the ) before the semicolon.
Q2
B — The comparison operator is written backwards. In JavaScript 'greater than or equal to' is >=, not =>, so the outer if should be if (score >= 50) {.
Q3
C — The inner if has lost its opening brace. Each if needs its own braces, so it should be if (temp > 35) { to match its closing }.
Q4
B — JavaScript is case sensitive, so the keyword must be a lowercase if. It should be if (level >= 5) {.
Q5
A — A variable is given its value with a single =. The == symbol is for comparing, so the line should be let lives = 3;.