Logical operators AND, OR and NOT

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

Logical operators AND, OR and NOT

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which condition is TRUE when Mark is 50 and the condition is Mark >= 0 AND Mark <= 100?
A TRUE, because both conditions are TRUE
B FALSE, because AND needs only one condition
2. Answer is 150. What does Answer < 0 OR Answer > 100 give?
A TRUE, because Answer > 100 is TRUE
B FALSE, because Answer < 0 is FALSE
3. Which range check correctly rejects marks outside 0 to 100?
A Mark < 0 AND Mark > 100
B Mark < 0 OR Mark > 100
4. GameOver is TRUE. What is the value of NOT GameOver?
A FALSE
B TRUE
5. Which loop condition keeps the game running while the game is not over?
A WHILE GameOver DO
B WHILE NOT GameOver DO

Match the Pairs

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

Left columnRight column
1Mark >= 0 AND Mark <= 100 AReverses the BOOLEAN value: TRUE when GameOver is FALSE, FALSE when GameOver is TRUE
2NOT GameOver BTRUE only when Mark is at least 0 and also at most 100, so it checks a valid range
3OR is TRUE when at least one condition is TRUE, so it can detect a value outside a range CAnswer < 0 OR Answer > 100
4Brackets make a complex condition clear by showing which part is worked out first DAND is the wrong operator. No Mark can be below 0 and above 100 at the same time, so the condition is never TRUE. It should use OR.
5IF Mark < 0 AND Mark > 100 THEN EOR is the wrong operator for a valid range. Every number is at least 0 or at most 100, so the condition is always TRUE. It should use AND.
6IF Mark >= 0 OR Mark <= 100 THEN F(Age >= 13 AND Age <= 19) OR Student = TRUE

Definitions

AND OR NOT

Fill in the Blanks

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

1.
IF Mark >= 0 ___ Mark <= 100 THEN OUTPUT "Valid mark" ENDIF
2.
IF Answer < 0 __ Answer > 100 THEN OUTPUT "Out of range" ENDIF
3.
WHILE ___ GameOver DO OUTPUT "Keep playing" ENDWHILE
4.
IF ___ (Mark >= 0 AND Mark <= 100) THEN OUTPUT "Invalid" ENDIF
5.
IF Age >= 18 AND (HasId __ HasPass) THEN OUTPUT "Entry allowed" ENDIF

Spot the Error

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

Question 1
A INPUT Mark
B IF Mark >= 0 OR Mark <= 100
C THEN
D OUTPUT "Valid"
E ENDIF
Question 2
A DECLARE Mark : INTEGER
B DECLARE Ok : BOOLEAN
C INPUT Mark
D Ok ← Mark > 0 AND Mark <= 100
Question 3
A GameOver ← FALSE
B Lives ← 3
C WHILE GameOver DO
D INPUT Lives
E GameOver ← Lives = 0
F ENDWHILE
Question 4
A INPUT Answer
B IF Answer < 0 AND Answer > 100
C THEN
D OUTPUT "Out of range"
E ENDIF
Question 5
A INPUT Rain
B INPUT Cold
C IF NOT Rain AND Cold
D THEN
E OUTPUT "Go out"
F ENDIF

Answer Key

Logical operators AND, OR and NOT
Section A: Quick Questions
Q1
A — TRUE, because both conditions are TRUE
Q2
A — TRUE, because Answer > 100 is TRUE
Q3
B — Mark < 0 OR Mark > 100
Q4
A — FALSE
Q5
B — WHILE NOT GameOver DO
Section B: Match the Pairs
Q1
Mark >= 0 AND Mark <= 100 → TRUE only when Mark is at least 0 and also at most 100, so it checks a valid range
Q2
NOT GameOver → Reverses the BOOLEAN value: TRUE when GameOver is FALSE, FALSE when GameOver is TRUE
Q3
OR is TRUE when at least one condition is TRUE, so it can detect a value outside a range → Answer < 0 OR Answer > 100
Q4
Brackets make a complex condition clear by showing which part is worked out first → (Age >= 13 AND Age <= 19) OR Student = TRUE
Q5
IF Mark < 0 AND Mark > 100 THEN → AND is the wrong operator. No Mark can be below 0 and above 100 at the same time, so the condition is never TRUE. It should use OR.
Q6
IF Mark >= 0 OR Mark <= 100 THEN → OR is the wrong operator for a valid range. Every number is at least 0 or at most 100, so the condition is always TRUE. It should use AND.
Section C: Definitions
Q1
AND OR NOT — AND is TRUE only when both conditions are TRUE. OR is TRUE when at least one is TRUE. NOT reverses a BOOLEAN value. Use brackets to make complex conditions clear.
Section D: Fill in the Blanks
Q1
IF Mark >= 0 AND Mark <= 100 THEN OUTPUT "Valid mark" ENDIF
Q2
IF Answer < 0 OR Answer > 100 THEN OUTPUT "Out of range" ENDIF
Q3
WHILE NOT GameOver DO OUTPUT "Keep playing" ENDWHILE
Q4
IF NOT (Mark >= 0 AND Mark <= 100) THEN OUTPUT "Invalid" ENDIF
Q5
IF Age >= 18 AND (HasId OR HasPass) THEN OUTPUT "Entry allowed" ENDIF
Section E: Spot the Error
Q1
B — OR is TRUE when at least one condition is TRUE, so every number passes (for example 150 is >= 0). A range check needs AND so that both conditions must be TRUE: IF Mark >= 0 AND Mark <= 100.
Q2
D — Mark > 0 leaves out 0, which is a valid mark. The first part must be Mark >= 0 so that both 0 and 100 are accepted: Ok ← Mark >= 0 AND Mark <= 100.
Q3
C — GameOver starts as FALSE, so WHILE GameOver is FALSE straight away and the loop never runs. NOT reverses the value so the loop runs while the game is not over: WHILE NOT GameOver DO.
Q4
B — No number can be less than 0 and more than 100 at the same time, so AND is never TRUE. To catch a value outside the range you need OR: IF Answer < 0 OR Answer > 100.
Q5
C — Without brackets, NOT only reverses Rain, not the whole condition. The brackets make NOT apply to (Rain AND Cold): IF NOT (Rain AND Cold).