Software Design and Development

Home > Software Design and Development > Core > Software Development Cycle > Detecting and correcting errors

Detecting and correcting errors

This material differentiates between the types of errors encountered during the testing phase, lists some common causes of each type of error and provides practise in detecting and correcting errors.

Syllabus outcomes

H4.2 A student applies appropriate development methods to solve software problems.

H4.3 A student applies a modular approach to implement well structured software solutions and evaluates their effectiveness.

Students learn to differentiate between the types of errors encountered during the testing phase and to recognise the cause of a specific error and determine how to correct it (SDD Syllabus, p.43).

The following headings may help you navigate:

Syntax errors

Syntax errors are written statements that do not conform to the rules of the language. The rules of the language are shown in metalanguages (BNF, EBNF or railroad diagrams) that describe the language.

Most modern programming environments have syntax prompts and auto-code completion features which help reduce the number of syntax errors made. Built-in debuggers assist in tracing code and locating errors.

Syntax errors can be caused by:

Activity 1

There are five syntax errors in the Visual Basic code below. See how many you can find.

Visual Basic code

Check your answers

Activity 1 PHP

For the following sections of PHP code find and fix the Syntax errors:

CODE Error Fixed code
Echo “Hello there”; Spelling of function Echo “Hello there”;
IF ($age < 13){
      Echo “Child”
}
1 error  
IFS (age < 13){
      Echo “Child”;
}
2 errors  
$count = 0;
WHILE count < 10
      $count = $count + 1;
      Echo “$count “;
}
2 errors  

Check your answers

Go To Top

Logic errors

Logic errors have occurred when incorrect algorithms result in unexpected output. Logic errors should be detected by desk checking the algorithm with carefully chosen test data. Peer checking by a colleague is another way to locate logic errors.

Activity 2

Trace the Visual Basic source code below to find the logic error.

Private Sub cmdPrintSequence_Click()
    Dim number As Integer

    number= 11

    Do

        picSequence.Print number

        number = number - 3

    Loop Until number = 0

End Sub

Check your answers

Activity 2 PHP

For the following lines of PHP determine the output of the code and explain and fix the logic errors:

  1. The following algorithms print out the numbers 1 to 5
    (Expected output is 1, 2, 3, 4, 5)

    Code Output Explain and fix the error
    $count = 0;
    WHILE ($count < 3) {
          $count = $count + 1;
          Echo “ $count”;
    }
       
    $count = 0;
    WHILE ($count < 5) {
          Echo “ $count;
    }
       
    $index = 0;
    WHILE ($index < 5) {
          $count = $count + 1;
          Echo “ $count”;
    }
       
    $count = 0;
    WHILE ($count < 5) {
          Echo “ $count
    }
       
    $count = 0;
    WHILE ($count < 5) {
          $count = $count - 1;
          Echo “ $count”;
    }
       

  2. The following code calculates the average of 2 numbers
    (test data, enter 6, 4 should display 5)

    Code Output Explain and fix the error
    $average = $num1 + $num2 / 2;
    Echo “The average is”; $average”;
       

  3. The following algorithms print out if someone is able to legally vote

    Test Data Output
    14 Not legal
    25 Legal
    18 Legal

    Code Explain and fix the error
    IF ($age > 18) {
          Echo “Legal”;
    }ELSE{
          Echo “Not legal”;
    }
     

Check your answers

Go To Top

Correcting logic errors

Logic errors can be difficult to locate and fix. Careful hand checking of the original algorithm with carefully chosen test data is generally the most efficient way to find logic errors.

Most programming environments have a debugger which allows the setting of breakpoints, watch expressions to observe variables or single line step routines.

Programmers can also add temporary output statements to observe what is happening.

Well-modularised code is easier to debug for logic errors.

Activity 3

Here is another example of Visual Basic code containing a logic error. Can you debug it? How might this error be fixed?

Private Sub cmdSum_Click()
     Dim number As Integer

     Dim sum As Integer

     number = Val(InputBox("Enter the integer number to be summed", number))

     sum = 0

Do

     number = number – 1

     sum = sum + number

Loop Until number = 0

     picDisplay Print "The sum of all integers between 0 and your number = " & sum

End Sub

Check your answers

Go To Top

Run-time errors

Run-time errors cause a program to fail or crash after it has been compiled and executed. Run-time errors can be very difficult to locate. Run-time errors are located by running the program with a well-chosen range of test data. Beta version testing involves independent operators who try to find as many execution errors as possible.

Causes of run-time errors

Common causes of run-time errors include:

Correcting run-time errors

Run-time errors can be very difficult to locate so all programs need thorough testing. Many run-time errors can be avoided using error-handling routines.

In Visual Basic 6.0, for example

on Error GoTo ErrorHandler
                  [Your code here]
Exit Sub      Prevents ErrorHandler executing if no error
ErrorHandler:
                  [error-handling routine - including a message]
Resume      Re-executes code that caused the error
                  (Resume Next jumps to the line following the error)
End Sub

Activity 4 PHP

Explain and fix the run-time error of the following PHP code

Code Output Explain and fix the error
$count = 0;
WHILE ($count < 5) {
            Echo “ $count”;
}
   

Check your answers

This work was prepared by Beverley Sampford

Go To Top



Neals logo | Copyright | Disclaimer | Contact Us | Help