Home > Software Design and Development > Core > Software Development Cycle > Detecting and correcting errors
The five errors are corrected and explained in red:
| Private Sub cmdSkillsTest_Click() | ' Math skills test |
|---|---|
| Dim lngBefore As Long Dim lngAfter As Long Dim lngTimeDiff As Long Dim strAns As String Dim intResult As Integer lngBefore = Timer Do |
‘Missing keyword in grouping ‘VB variables cannot start with ‘digit (2time) ' Save seconds since midnight |
| strAns = InputBox("What is 150 + 235?","Hurry”) | ‘Missing‘ punctuation |
| Loop Until Val(strAns) = 385 lngAfter = Timer |
‘Typing mistake in reserved word ' Save seconds since midnight |
| ‘The difference between the stored time values is how many seconds the user took to answer the question. | |
| lngTimeDiff = lngAfter
– lngBefore ‘Variable name typing error intResult = MsgBox ("That took you only "& Str(lngTimeDiff) & " seconds!") |
|
| End Sub | |
Congratulations if you found all five errors! You have sharp eyes.
| CODE | Error | Fixed code |
|---|---|---|
| Ehco “Hello there”; | Spelling of function | Echo “Hello there”; |
| IF ($age < 13){ Echo “Child” } |
1 error Missing semi-colon ; that end each line of code |
IF ($age < 13){ Echo “Child”; } |
| IFS (age < 13){ Echo “Child”; } |
2 errors Key work IF miss-spelt Variable age must have a $ |
IF ($age < 13){ Echo “Child”; } |
| $count = 0; WHILE count < 10 $count = $count + 1; Echo “count “; } |
2 errors Condition of the loop must be within brackets ( ) Curly brackets missing |
$count = 0; WHILE (count < 10){ $count = $count + 1; Echo “$count “; } |
The hand trace shows the value stored in number has gone to –1 without ever becoming zero. An endless loop has beencreated.
| Loop Iteration | number | picSequence |
|---|---|---|
| 0 | 11 | None |
| 1 | 8 | 11 |
| 2 | 5 | 11 8 |
| 3 | 2 | 11 8 5 |
| 4 | -1 | 11 8 5 2 |
| Code | Output | Explain and fix the error |
|---|---|---|
| $count = 0; WHILE ($count < 3) { $count = $count + 1; Echo “ $count”; } |
1 2 3 |
Loop only executed 3 times, change condition to count < 5 |
| $count = 0; WHILE ($count < 5) { Echo “ $count; } |
0 0 0 0 ... endless loop |
Endless loop as the variable count is not incremented within the loop Add count = count + 1 |
| $index = 0; WHILE ($index < 5) { $count = $count + 1; Echo “ $count”; } |
0 0 0 0 ...endless loop |
Variable names change. Change all index to count |
| $count = 0; WHILE ($count < 5) { Echo “ $count } |
0 1 2 3 4 |
Variable is ouputted before be incremeted. Either set count to 1, or move count=count+1 to above ECHO |
| $count = 0; WHILE ($count < 5) { $count = $count - 1; Echo “ $count”; } |
-1 |
Varible is incremented incorrectly. Change to $count=$count+1 |
| Code | Output | Explain and fix the error |
|---|---|---|
| $average = $num1 + $num2 / 2; Echo “The average is”; $average”; |
8 | Average=6 + 4/2 |
| Echo "The average is"; $average"; | Average = 6 + 2 2 values are not added before dividing by 2 Add brackets $average = ($num1 + $num2) / 2; |
| 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”; } |
When age=18, Not legal is incorrectly displayed. Change condition to ($age >= 18) |
Desk checking the original algorithm with a value like 4 (sum = 4+3+2+1 = 10) should have revealed the error. This code gives a sum of 6.
The hand execution results are shown below.
The problem is fixed by placing the line number = number - 1 after the sum = sum + number line.
| Loop iteration | number | sum | expected sum |
|---|---|---|---|
| 0 | 4 | 0 | |
| 1 | 3 | 3 | 4 |
| 2 | 2 | 5 | 7 |
| 3 | 1 | 6 | 9 |
| 4 | 0 | 6 | 10 |
| Code | Output | Explain and fix the error |
|---|---|---|
| $count = 0; WHILE ($count < 5) { Echo “ $count”; } |
0 0 0 0 ...endless loop |
The endless loop could cause an overflow error if $count is set as an integer where there is a limit ti the value it can hold. Add $count = $count + 1 to increment the count. |