Logic Errors/Flaws

These are the hardest errors to find as they do not halt the program. They arise from faulty thinking on behalf of the programmer. They can be very troublesome.

These are mistakes in a program’s logic.

var Number1 = 0 ;
var Number2 = 0 ;
var Total = 0 ;

Number1 = prompt ('Enter first number: ') ;
Number1 = parseInt (Number1) ;
Number2 = prompt ('Enter second number: ') ;
Number2 = parseInt (Number2) ;

Total = Number1 + Number1 ;

alert ('The Total = ' + Total) ;

The logic error or flaw in the above code extract is that the programmer has accidentally typed Number1 instead of Number2. Although this is an error it will not stop the program running because it is not a syntax error it is a logic error. Therefore, if the data input was Number1 = 5 and Number2 = 20 the answer given would be 10 rather than 25 as it should be.

Programs with logic errors will often translate with no visible errors, and will execute and output results. However, at least some of the time the output will be incorrect. Error messages will generally not appear if a logic error occurs, this makes logic errors very difficult to locate and correct.

Next: Top Ten Programming Errors