Syntax & Logic Errors Flashcards
All 7 cards in this deck
What is a logic error?
An error where the program runs but does not do what is expected, e.g. it gives an incorrect or unexpected output.
Give two examples of syntax errors in code.
Misspelling a keyword (e.g.
OUPTUTforOUTPUT) and a missing bracket or quotation mark.
Also: using a keyword such as FOR as a variable name, or spaces in a variable name.Give two examples of logic errors in code.
Using the wrong operator (e.g.
<where≤is needed, orANDinstead ofOR) and creating an infinite loop.
Also: brackets in the wrong place in a calculation, or a loop that runs the wrong number of times.True or false? A program containing a syntax error will still run but give the wrong answer.
False. A program will not run with a syntax error; it is a logic error that runs but gives incorrect results.
What are the steps to identify and correct a logic error in a given algorithm?
e.g. line 3 reads
total ← num1 - num2but the two numbers should be added- Compare what each line does with what the algorithm should do: line 3 subtracts
- State the line number: line 3
- Give the corrected line, or an unambiguous description of the change:
total ← num1 + num2
What is a syntax error?
A mistake in the grammar of the code: it breaks the rules of the programming language, so the program cannot be translated or run.
Why can't a translator detect a logic error?
Code with a logic error still obeys the language's rules, so it translates and runs. Logic errors are found by testing or a trace table instead.