Error: wrong logical operator

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

Error: wrong logical operator

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which condition correctly checks for an invalid mark (outside 0 to 100)?
A Mark < 0 OR Mark > 100
B Mark < 0 AND Mark > 100
2. Mark < 0 AND Mark > 100 is used to catch invalid marks. What is the problem?
A It is never TRUE, so no invalid mark is caught
B It is always TRUE, so every mark is rejected
3. Mark ← 150. What does Mark < 0 AND Mark > 100 evaluate to?
A FALSE
B TRUE
4. Mark ← 150. What does Mark < 0 OR Mark > 100 evaluate to?
A FALSE
B TRUE
5. You test Mark < 0 OR Mark > 100 with the valid value 50. What result should you get?
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 Mark < 0 AND Mark > 100 THEN AWrong logical operator: OR is used where AND is needed, so every number counts as valid, even -5 or 120. Fix: use AND.
2IF Mark < 0 OR Mark > 100 THEN BFaulty condition: no mark is below 0 and above 100 at the same time, so it is never TRUE and invalid marks are missed.
3Testing the faulty condition with an invalid value of 120 still gives FALSE, which shows the logic is wrong. CMark ← 120 IF Mark < 0 AND Mark > 100 THEN
4To correct the error, change AND to OR so that either invalid case triggers the message. DWrong logical operator: AND is used where OR is needed, so the condition is never TRUE and no invalid mark is ever reported. Fix: use OR.
5IF Mark < 0 AND Mark > 100 THEN OUTPUT "Invalid mark" ENDIF ECorrect condition: TRUE for an invalid mark, because OR is TRUE when either part is TRUE.
6IF Mark >= 0 OR Mark <= 100 THEN OUTPUT "Valid mark" ENDIF FIF Mark < 0 OR Mark > 100 THEN OUTPUT "Invalid mark" ENDIF

Definitions

Mark < 0 OR Mark > 100

Fill in the Blanks

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

1.
IF Mark < 0 __ Mark > 100 THEN
2.
IF Mark >= 0 ___ Mark <= 100 THEN OUTPUT "Valid" ENDIF
3.
WHILE Mark < 0 __ Mark > 100 DO INPUT Mark ENDWHILE
4.
Mark ← 120 OUTPUT Mark < 0 OR Mark > 100 // Output: ____
5.
IF Mark < 0 OR Mark _ 100 THEN OUTPUT "Invalid mark" ENDIF

Spot the Error

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

Question 1
A OUTPUT "Enter a mark"
B INPUT Mark
C IF Mark < 0 AND Mark > 100
D THEN
E OUTPUT "Invalid mark"
F ENDIF
Question 2
A IF Mark < 0 AND Mark > 100
B THEN
C OUTPUT "Invalid mark"
D ENDIF
Question 3
A INPUT Mark
B Valid ← TRUE
C // check the mark
D IF Mark < 0 AND Mark > 100
E THEN Valid ← FALSE
F ENDIF
Question 4
A Mark ← 150
B IF Mark < 0 AND Mark > 100
C THEN OUTPUT "Invalid"
D ENDIF
Question 5
A Bad ← Mark < 0 AND Mark > 100
B IF Bad = TRUE
C THEN OUTPUT "Invalid"
D ENDIF

Answer Key

Error: wrong logical operator
Section A: Quick Questions
Q1
A — Mark < 0 OR Mark > 100
Q2
A — It is never TRUE, so no invalid mark is caught
Q3
A — FALSE
Q4
B — TRUE
Q5
B — FALSE
Section B: Match the Pairs
Q1
IF Mark < 0 AND Mark > 100 THEN → Faulty condition: no mark is below 0 and above 100 at the same time, so it is never TRUE and invalid marks are missed.
Q2
IF Mark < 0 OR Mark > 100 THEN → Correct condition: TRUE for an invalid mark, because OR is TRUE when either part is TRUE.
Q3
Testing the faulty condition with an invalid value of 120 still gives FALSE, which shows the logic is wrong. → Mark ← 120 IF Mark < 0 AND Mark > 100 THEN
Q4
To correct the error, change AND to OR so that either invalid case triggers the message. → IF Mark < 0 OR Mark > 100 THEN OUTPUT "Invalid mark" ENDIF
Q5
IF Mark < 0 AND Mark > 100 THEN OUTPUT "Invalid mark" ENDIF → Wrong logical operator: AND is used where OR is needed, so the condition is never TRUE and no invalid mark is ever reported. Fix: use OR.
Q6
IF Mark >= 0 OR Mark <= 100 THEN OUTPUT "Valid mark" ENDIF → Wrong logical operator: OR is used where AND is needed, so every number counts as valid, even -5 or 120. Fix: use AND.
Section C: Definitions
Q1
Mark < 0 OR Mark > 100 — Use OR, not AND, to catch an invalid mark. A mark cannot be below 0 and above 100 at once, so AND is never TRUE. OR is TRUE when either part is TRUE.
Section D: Fill in the Blanks
Q1
IF Mark < 0 OR Mark > 100 THEN
Q2
IF Mark >= 0 AND Mark <= 100 THEN OUTPUT "Valid" ENDIF
Q3
WHILE Mark < 0 OR Mark > 100 DO INPUT Mark ENDWHILE
Q4
Mark ← 120 OUTPUT Mark < 0 OR Mark > 100 // Output: TRUE
Q5
IF Mark < 0 OR Mark > 100 THEN OUTPUT "Invalid mark" ENDIF
Section E: Spot the Error
Q1
C — A mark cannot be below 0 and above 100 at the same time, so AND is never TRUE and invalid marks are never caught. It should be IF Mark < 0 OR Mark > 100.
Q2
A — AND needs both parts to be TRUE, which is impossible for one mark. A mark of 150 or -5 would slip through. Use OR so that either part being TRUE catches the invalid mark.
Q3
D — With AND the condition is never TRUE, so Valid stays TRUE even for a mark such as 120. It should be OR so that either an out-of-range low or high mark sets Valid to FALSE.
Q4
B — Testing with the invalid value 150 shows the fault: 150 > 100 is TRUE but 150 < 0 is FALSE, so AND gives FALSE and nothing is output. Changing AND to OR makes the program output Invalid.
Q5
A — Bad should be TRUE when the mark is below 0 or above 100. With AND it is always FALSE, because no mark can be both. It should be OR.