Logic error versus syntax error

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

Logic error versus syntax error

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. An algorithm runs to the end with no warning, but the output is wrong. What kind of error is this?
A Logic error
B Syntax error
2. Which line has a logic error if it should work out the area of a rectangle? area ← width + height
A It runs fine but adds when it should multiply, so the output is wrong
B It breaks the writing rules, so the algorithm cannot run
3. How can you find a logic error in an algorithm?
A Wait for a warning message to tell you where it is
B Trace it or test it and compare the actual output with the expected output
4. Which one is a syntax error?
A OUTPUT total with a missing closing quote in OUTPUT "Total is
B total ← 0 at the start, then total ← total - score to add up scores
5. A flowchart for a pass mark check has the Yes and No arrows swapped. The flowchart can be followed from Start to End. What type of error is it?
A Syntax error
B Logic error

Match the Pairs

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

Left columnRight column
1total ← 0 FOR each mark: total ← total + mark OUTPUT total / 3 (should divide by 4 marks) AThis is a syntax error. The stray bracket breaks the writing rules, so the algorithm is not written correctly. It is not a logic error.
2OUTPUT "Total is" total ) BA logic error: the algorithm runs to the end with no warning, but the average it outputs is wrong.
3A logic error gives no warning. The algorithm finishes but the answer is wrong, like a loop that adds 1 when it should add 2. CThis is a logic error. It runs with no warning, but a 13 year old gets the wrong output. Testing with age 13 shows it.
4A flowchart logic error: the decision arrows for Yes and No are swapped, so the chart works but gives the wrong result. Dcount ← 0 REPEAT 5 TIMES count ← count + 1 OUTPUT count (should count by 2s)
5IF age >= 13 THEN OUTPUT "Teen" ELSE OUTPUT "Child" (should be age >= 13 only for teen, but the test uses age > 13) EDecision: score >= 50? Yes → OUTPUT "Fail" No → OUTPUT "Pass"
6IF score > 50 THEN OUTPUT "Pass" ELSE OUTPUT "Fail" ENDIF ) FA syntax error: the line breaks the writing rules because of the stray bracket, so it cannot be followed.

Definitions

logic error

Fill in the Blanks

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

1.
area ← 4 + 3 OUTPUT area // Runs, wrong output: _____ error
2.
OUTPUT "Hello // Missing quote breaks the writing rules // This is a ______ error
3.
// Using > here is a logic error at 13 IF age __ 13 THEN OUTPUT "Teen"
4.
x ← 2 + 3 OUTPUT x // Only tracing or _______ shows the bug
5.
Decision: mark >= 50 ? __: OUTPUT "Fail" // Swapping Yes and No is a logic error

Spot the Error

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

Question 1
A total ← 0
B total ← 10
C total ← total + score
D OUTPUT total
Question 2
A INPUT age
B IF age >= 13 THEN
C IF age > 13 THEN
D ENDIF
Question 3
A INPUT a
B INPUT b
C avg ← a + b / 2
D OUTPUT avg
Question 4
A SET name TO "Sam"
B SET score TO 9
C OUTPUT name
D OUTPUT "Done
Question 5
A total ← 0
B REPEAT 4 TIMES
C INPUT n
D total ← n
E END REPEAT

Answer Key

Logic error versus syntax error
Section A: Quick Questions
Q1
A — Logic error
Q2
A — It runs fine but adds when it should multiply, so the output is wrong
Q3
B — Trace it or test it and compare the actual output with the expected output
Q4
A — OUTPUT total with a missing closing quote in OUTPUT "Total is
Q5
B — Logic error
Section B: Match the Pairs
Q1
total ← 0 FOR each mark: total ← total + mark OUTPUT total / 3 (should divide by 4 marks) → A logic error: the algorithm runs to the end with no warning, but the average it outputs is wrong.
Q2
OUTPUT "Total is" total ) → A syntax error: the line breaks the writing rules because of the stray bracket, so it cannot be followed.
Q3
A logic error gives no warning. The algorithm finishes but the answer is wrong, like a loop that adds 1 when it should add 2. → count ← 0 REPEAT 5 TIMES count ← count + 1 OUTPUT count (should count by 2s)
Q4
A flowchart logic error: the decision arrows for Yes and No are swapped, so the chart works but gives the wrong result. → Decision: score >= 50? Yes → OUTPUT "Fail" No → OUTPUT "Pass"
Q5
IF age >= 13 THEN OUTPUT "Teen" ELSE OUTPUT "Child" (should be age >= 13 only for teen, but the test uses age > 13) → This is a logic error. It runs with no warning, but a 13 year old gets the wrong output. Testing with age 13 shows it.
Q6
IF score > 50 THEN OUTPUT "Pass" ELSE OUTPUT "Fail" ENDIF ) → This is a syntax error. The stray bracket breaks the writing rules, so the algorithm is not written correctly. It is not a logic error.
Section C: Definitions
Q1
logic error — A mistake in an algorithm's steps. It still runs to the end with no warning, but gives the wrong output. Only tracing or testing shows it, unlike a syntax error that breaks the writing rules.
Section D: Fill in the Blanks
Q1
area ← 4 + 3 OUTPUT area // Runs, wrong output: logic error
Q2
OUTPUT "Hello // Missing quote breaks the writing rules // This is a syntax error
Q3
// Using > here is a logic error at 13 IF age >= 13 THEN OUTPUT "Teen"
Q4
x ← 2 + 3 OUTPUT x // Only tracing or testing shows the bug
Q5
Decision: mark >= 50 ? No: OUTPUT "Fail" // Swapping Yes and No is a logic error
Section E: Spot the Error
Q1
B — This is a logic error. The total should start at 0. Starting at 10 means the program still runs to the end with no warning, but the output is always 10 too high. A trace table would show it.
Q2
C — This is a logic error. Using > instead of >= means a 13 year old gets no output. The program runs with no warning but gives the wrong result. Testing with the value 13 reveals it.
Q3
C — This is a logic error. Without brackets, b is divided by 2 first and then added to a. The program runs without any warning but outputs the wrong average. It should be avg ← (a + b) / 2.
Q4
D — This is a syntax error. The closing quote is missing, so the line breaks the writing rules and the algorithm cannot run. A logic error would run to the end. It should be OUTPUT "Done".
Q5
D — This is a logic error. The total is replaced each time instead of having n added to it. The loop still finishes with no warning, but the output is only the last number. Tracing total on each loop shows it.