3.2.11 - Finding and correcting program errors (C#)

3.2.11 - Finding and correcting program errors (C#)

Distinguish syntax faults from incorrect logic, find their causes and verify a correction against the problem. Required written answers use C#. Native execution is unavailable here; the runnable activity is an optional Python demonstration.

Syntax errors

A syntax error breaks a programming language's grammatical rules, preventing the source from being accepted as valid code.

Syntax error

An error in the form of a program statement, such as a missing required delimiter or an incorrectly written keyword.

C#
Console.WriteLine("Hello";

The closing parenthesis is missing. The corrected statement is:

C#
Console.WriteLine("Hello");

C# also requires appropriate braces around multi-statement bodies, correctly formed declarations and valid identifiers. In an IDE, a compiler message can suggest a location, but the actual cause may be a preceding missing quote or brace. Inspect the structure rather than inserting punctuation blindly. Indentation helps people follow nesting, while braces determine C# block structure.

Written-paper marking tolerates minor syntax mistakes when the intended logic remains clear. That fairness rule does not make malformed source runnable: learn valid native syntax while explaining the logic accurately.

Logic errors

A logic error gives the wrong behaviour for the problem even though the statements may be valid in the programming language.

C#
int quantity = 3;
int price = 4;
int cost = quantity + price;
Console.WriteLine(cost);

This runs and displays 7, but the required price for three £4 items is 12. The arithmetic must multiply. A wrong boundary comparison, incorrect AND/OR combination, misplaced initialisation or missing loop update can likewise express the wrong algorithm.

For an average, double average = (first + second) / 2; uses integer division if both inputs are integers. With 3 and 4 it stores 3.0 instead of 3.5. Changing the divisor to 2.0 repairs the processing; merely choosing a double destination does not undo earlier integer division.

Trace variables through the failing input to identify the earliest difference from expected behaviour. A crash such as an out-of-bounds access is not evidence that the source grammar is wrong: a syntactically valid loop can still compute an invalid index. Classify the actual fault, then explain its consequence.

Continue this lesson.

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

Create free account