Logic error

From Higher Computing Science
Jump to: navigation, search

This article is unfinished. Please consider joining and adding to this article. Read about Page layout beforehand.

Key points

  • A logic error is an error in the program’s decision making. This is caused by the programmer. The program will run, but not carry out instructions in the intended way, because the logic of the program is wrong.
  • Common causes of logic errors are when calculations are wrong and incorrect use of IF statements.

Information

VbLogicErrorExample.png

This example shows a piece of Visual Basic code. This program will accept 3 values from the user and calculate the average. However whenever this program is executed, the average is wrong. If the user enters the values 10, 20 and 30, the program should display 20, instead it would display 40! This is because the logic is wrong. A common cause of logic errors is when a calculation is incorrect, which is true in this case.

The calculation:

average=value1+value2+value3/3

Is wrong, as it won't divide the total of the 3 values by 3, it will only divide the last value. To correct this we should change it to be:

average=(value1+value2+value3)/3

Videos

Further information

Test yourself

Teaching resources