IF with a compound condition

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

IF with a compound condition

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which IF checks that Age is from 13 to 19 inclusive (a teenager)?
A IF Age >= 13 OR Age <= 19 THEN
B IF Age >= 13 AND Age <= 19 THEN
2. Which IF accepts either a capital or a lowercase Y?
A IF Answer = "Y" OR Answer = "y" THEN
B IF Answer = "Y" AND Answer = "y" THEN
3. True or false: in IF Score > 50 AND Score < 80 THEN, the condition is true when Score is 50.
A True
B False
4. Which single IF does the same job as two nested IFs: IF Age >= 13 THEN, then IF Age <= 19 THEN?
A IF Age >= 13 AND Age <= 19 THEN
B IF Age >= 13 OR Age <= 19 THEN
5. A cinema gives a discount to children under 12 or people over 65. Which condition is correct?
A IF Age < 12 OR Age > 65 THEN
B IF Age < 12 AND Age > 65 THEN

Match the Pairs

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

Left columnRight column
1IF Age >= 13 AND Age <= 19 THEN ARuns the code when the answer is a capital Y or a lowercase y, because only one test needs to be true
2IF Answer = "Y" OR Answer = "y" THEN BRuns the code only when Age is between 13 and 19 inclusive, because both tests must be true
3One IF with AND can replace two nested IFs that must both be true CIF Day = "Sat" OR Day = "Sun" THEN
4Use OR when any one of several rules is enough to run the code DThe second test is missing the variable name. Each side of AND needs a full test, so it should be Age <= 19.
5IF Age >= 13 AND <= 19 THEN EIF Score >= 50 AND Attendance >= 90 THEN
6IF Answer = "Y" AND Answer = "y" THEN FAND is the wrong operator. Answer cannot be both Y and y at once, so the condition is never true. It should use OR.

Definitions

IF Age >= 13 AND Age <= 19 THEN

Fill in the Blanks

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

1.
IF Age >= 13 ___ Age <= 19 THEN
2.
IF Answer = "Y" __ Answer = "y" THEN
3.
IF Age >= 13 AND Age __ 19 THEN OUTPUT "Teenager" ENDIF
4.
IF ___ (Age < 13) THEN OUTPUT "Not a child" ENDIF
5.
IF Score < 0 __ Score > 100 THEN OUTPUT "Invalid" ENDIF

Spot the Error

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

Question 1
A INPUT Age
B INPUT Ok
C // Both must be true
D IF Age >= 8 AND Ok = "Y"
E OUTPUT "Enter"
F ENDIF
Question 2
A INPUT Age
B // Teenager: 13 to 19
C IF Age >= 13 AND <= 19 THEN
D OUTPUT "Teenager"
E ENDIF
Question 3
A INPUT Ans
B IF Ans = "Y" OR "y" THEN
C OUTPUT "Continue"
D ENDIF
Question 4
A IF Mk >= 0 OR Mk <= 100 THEN
B OUTPUT "Valid"
C ELSE
D OUTPUT "Invalid"
E ENDIF
Question 5
A IF Age NOT < 13 THEN
B OUTPUT "13 or older"
C ENDIF

Answer Key

IF with a compound condition
Section A: Quick Questions
Q1
B — IF Age >= 13 AND Age <= 19 THEN
Q2
A — IF Answer = "Y" OR Answer = "y" THEN
Q3
B — False
Q4
A — IF Age >= 13 AND Age <= 19 THEN
Q5
A — IF Age < 12 OR Age > 65 THEN
Section B: Match the Pairs
Q1
IF Age >= 13 AND Age <= 19 THEN → Runs the code only when Age is between 13 and 19 inclusive, because both tests must be true
Q2
IF Answer = "Y" OR Answer = "y" THEN → Runs the code when the answer is a capital Y or a lowercase y, because only one test needs to be true
Q3
One IF with AND can replace two nested IFs that must both be true → IF Score >= 50 AND Attendance >= 90 THEN
Q4
Use OR when any one of several rules is enough to run the code → IF Day = "Sat" OR Day = "Sun" THEN
Q5
IF Age >= 13 AND <= 19 THEN → The second test is missing the variable name. Each side of AND needs a full test, so it should be Age <= 19.
Q6
IF Answer = "Y" AND Answer = "y" THEN → AND is the wrong operator. Answer cannot be both Y and y at once, so the condition is never true. It should use OR.
Section C: Definitions
Q1
IF Age >= 13 AND Age <= 19 THEN — A compound condition joins tests with AND, OR or NOT so one IF checks several things at once. AND needs both tests true; OR needs at least one true.
Section D: Fill in the Blanks
Q1
IF Age >= 13 AND Age <= 19 THEN
Q2
IF Answer = "Y" OR Answer = "y" THEN
Q3
IF Age >= 13 AND Age <= 19 THEN OUTPUT "Teenager" ENDIF
Q4
IF NOT (Age < 13) THEN OUTPUT "Not a child" ENDIF
Q5
IF Score < 0 OR Score > 100 THEN OUTPUT "Invalid" ENDIF
Section E: Spot the Error
Q1
D — The IF line is missing THEN. A compound condition still needs THEN after it: IF Age >= 8 AND Ok = "Y" THEN.
Q2
C — Each side of AND must be a full test. <= 19 has no variable, so it should be Age <= 19.
Q3
B — OR cannot join a test to a bare value. Both sides need a full comparison: Ans = "Y" OR Ans = "y".
Q4
A — A valid mark must be at least 0 AND at most 100. With OR every number passes, so it must be AND.
Q5
A — NOT goes in front of the whole condition, not in the middle of a comparison. It should be IF NOT (Age < 13) THEN.