Compound conditions inside an if

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

Compound conditions inside an if

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which if checks that age is between 10 and 15 (including both)?
A if (age >= 10 || age <= 15)
B if (age >= 10 && age <= 15)
2. Which if is true when score is OUTSIDE the range 40 to 90?
A if (score < 40 && score > 90)
B if (score < 40 || score > 90)
3. What does this condition do? if (temp > 20 && temp < 30)
A It is true only when temp is between 20 and 30
B It is true for any temp value at all
4. x is 5. What is the result of (x > 3 && x < 4)?
A false
B true
5. x is 5. What is the result of (x < 3 || x > 4)?
A true
B false

Match the Pairs

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

Left columnRight column
1if (age >= 10 && age <= 15) A&& is the wrong operator for outside a range. No number is below 0 and above 40 at the same time, so it should be ||.
2if (score < 0 || score > 100) BThe second test is missing the variable name. Each side of && must be a full condition, so it should be age <= 15.
3Use brackets so the OR is checked first, then the result is combined with AND CRuns when score is outside the range 0 to 100, because either test being true is enough
4Use ! to check that something is NOT true, together with another test Dif (age >= 10 && (house === "Red" || house === "Blue"))
5if (age >= 10 && <= 15) Eif (score >= 50 && !isAbsent)
6if (temp < 0 && temp > 40) FRuns only when age is between 10 and 15 inclusive, because both tests must be true

Definitions

if (age >= 10 && age <= 15)

Fill in the Blanks

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

1.
let age = 12; if (age >= 10 __ age <= 15) { console.log("In range");
2.
let age = 18; if (age < 10 __ age > 15) { console.log("Outside");
3.
let done = false; if (_done && score >= 50) { console.log("Keep going");
4.
if (isMember && (age < 10 __ age > 15)) { console.log("Special price"); }
5.
let temp = 25; if (temp >= 20 && ____ <= 30) { console.log("Nice day");

Spot the Error

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

Question 1
A let age = 12;
B // Is age from 10 to 15?
C if (age >= 10 || age <= 15) {
D console.log('Yes');
E }
Question 2
A let temp = 38;
B if (temp < 5 && temp > 35) {
C console.log('Extreme');
D }
Question 3
A let hour = 14;
B let open = false;
C // Open from 9 to 17
D if (hour >= 9 && < 17) {
E open = true;
F }
Question 4
A if (!isRaining && temp > 20 {
B console.log('Picnic!');
C }
Question 5
A let pts = 85;
B if (80 <= pts <= 100) {
C console.log('A');
D }

Answer Key

Compound conditions inside an if
Section A: Quick Questions
Q1
B — if (age >= 10 && age <= 15)
Q2
B — if (score < 40 || score > 90)
Q3
A — It is true only when temp is between 20 and 30
Q4
A — false
Q5
A — true
Section B: Match the Pairs
Q1
if (age >= 10 && age <= 15) → Runs only when age is between 10 and 15 inclusive, because both tests must be true
Q2
if (score < 0 || score > 100) → Runs when score is outside the range 0 to 100, because either test being true is enough
Q3
Use brackets so the OR is checked first, then the result is combined with AND → if (age >= 10 && (house === "Red" || house === "Blue"))
Q4
Use ! to check that something is NOT true, together with another test → if (score >= 50 && !isAbsent)
Q5
if (age >= 10 && <= 15) → The second test is missing the variable name. Each side of && must be a full condition, so it should be age <= 15.
Q6
if (temp < 0 && temp > 40) → && is the wrong operator for outside a range. No number is below 0 and above 40 at the same time, so it should be ||.
Section C: Definitions
Q1
if (age >= 10 && age <= 15) — A compound condition joins several tests in one if using && (and), the or operator and ! (not). Brackets set the order. Use it for range checks, like age between 10 and 15.
Section D: Fill in the Blanks
Q1
let age = 12; if (age >= 10 && age <= 15) { console.log("In range");
Q2
let age = 18; if (age < 10 || age > 15) { console.log("Outside");
Q3
let done = false; if (!done && score >= 50) { console.log("Keep going");
Q4
if (isMember && (age < 10 || age > 15)) { console.log("Special price"); }
Q5
let temp = 25; if (temp >= 20 && temp <= 30) { console.log("Nice day");
Section E: Spot the Error
Q1
C — A range check needs both tests to be true, so it must use &&. With ||, every number passes, because every number is at least 10 or at most 15. It should be: if (age >= 10 && age <= 15) {
Q2
B — To test for outside a range, use ||. No temperature can be below 5 and above 35 at the same time, so && is never true. It should be: if (temp < 5 || temp > 35) {
Q3
D — Each side of && must be a full test. The second test is missing the variable hour. It should be: if (hour >= 9 && hour < 17) {
Q4
A — The closing bracket of the condition is missing. The whole compound condition must sit inside matching brackets before the {. It should be: if (!isRaining && temp > 20) {
Q5
B — JavaScript cannot chain comparisons like this. It works out 80 <= pts first, which gives true or false, then compares that with 100. Write two tests joined by &&: if (pts >= 80 && pts <= 100) {