3.2.11 - Finding and correcting program errors (VB.NET)

3.2.11 - Finding and correcting program errors (VB.NET)

Distinguish syntax and logic faults, locate their causes and check a corrected VB.NET solution. Native code answers are written without Run; the optional Python demonstration makes one logic fault observable.

Syntax errors

A syntax error breaks the language's grammatical rules and prevents the code from being understood correctly by its translator.

Syntax error

An error in the grammatical form of a program statement, such as a missing required keyword or unmatched bracket.

In normal VB.NET source, this block is missing the required Then:

VB.NET
' Deliberately incorrect source
If score >= 5
    Console.WriteLine("Pass")
End If

The correction is If score >= 5 Then. Other examples include an unmatched quotation mark or an unfinished bracket in a call. Read a diagnostic message as a starting clue: the real omission may be just before the location reported. Correct the source, then check again for further errors.

Precise syntax remains good programming practice. Written exam marking may tolerate a minor missing keyword where logic is unambiguous; that does not mean a compiler will execute the incomplete source. Do not confuse a marking tolerance with native language syntax.

Logic errors

A logic error gives the wrong behaviour even when the statements have a valid grammatical form.

Logic error

A fault in a program's reasoning or processing that causes behaviour to differ from its requirements.

For a permitted range 1–10, this rejection condition is logically wrong:

VB.NET
If quantity < 1 And quantity > 10 Then
    Console.WriteLine("Reject")
End If

No number can be below 1 and above 10 at the same time. Quantity 0 is wrongly left unrejected. Use Or for the outside-range condition. The syntax is valid; the Boolean rule is wrong.

Other logic faults include subtracting instead of adding to a total, using a strict comparison at an inclusive boundary, and putting a total's reset inside a loop. A trace helps identify the first incorrect state rather than guessing from the final output. Explain the fault relative to the requirement: “adds only the last donation because the total resets each pass” is more useful than “the loop is wrong”.

Correcting and retesting a program

Correcting a program means changing the faulty cause and checking that the revised behaviour satisfies the requirements.

This valid-syntax fragment intends to sum three readings but replaces the total:

Continue this lesson.

You’re previewing NovaLearn without an account. Create a free account to read the rest.

Create free account