Process of detecting and correcting errors

There are three types of programming errors you need to understand.

Syntax error

Syntax is the structure of a language.  Each programming language has its own specific syntax.  All languages have their own keywords, variable creation conventions, statement terminations, etc. If a statement does not conform to the language’s syntax it will produce a syntax error when it is compiled. Most IDEs (Integrated Development Environments) contain a debugger that will identify syntax errors before you compile the program. Syntax errors in pseudocode are not common, mainly because there is no absolute syntax for pseudocode.  That said, spot the obvious syntax error in this pseudocode:

i ← 10

WHILE i > 5 DO
  OUTPUT (i, " i is greater than 5")
  i ← i - 1
END FOR

Logic error

Logic errors occur when you create code that does not do what it is meant to.  A logic error produces unexpected and unwanted outcomes when the program runs.   Here is an example:

i = 3
while i < 5:
    print("This will run forever..")

The iteration above will never execute because the variable i does not equal less than 5 and there is no statement to modify it.

Run-time error

A run-time error can only occur when a program has been compiled and is running. The program meets a problem it is not programmed to handle and ends unexpectadly (it crashes).  The three run-time errors you need to know are:

Arithmetic Overflow: A situation where a calculation exceeds the maximum size that a data type can hold. For instance, adding 1 to an 8-bit unsigned integer that already holds the value 255 results in an overflow because the maximum value it can represent is 255.

Division by Zero: Attempting to divide a number by zero, which is not allowed.

result = 10 / 0 # Raises a ZeroDivisionError exception.

Index Out of Range: Accessing an element of a list or array using an index that doesn’t exist within the list or array.

my_list = [1, 2, 3]
print(my_list[3]) # Raises an IndexError as the valid indices are 0, 1, and 2.

Methods of error detection and correction

Methodical approach to the isolation of logic errors: This refers to a systematic process of finding and fixing errors in the logical flow of a program’s code.

Use of debugging techniques

Breakpoints: Placing breakpoints in code allows developers to temporarily halt program execution at specific points. This is done to inspect the current state, such as variable values and program flow, to find issues.

In Python, breakpoints can be set using the built-in breakpoint() function.

Here is an example using the breakpoint() function:

def calculate_sum(numbers):
    sum = 0
    for number in numbers:
        sum += number
        breakpoint() # A breakpoint is set here for debugging
    return sum

When this function is called, execution will pause at the breakpoint.

However, in replit there is a dedicated debugger built in.

Print statements: Inserting print statements into code segments to output variable values and program flow indicators to the console. This simple technique can help pinpoint where things are not working as expected.

Desk Checking (Trace Tables): A manual process where a developer simulates the execution of code on paper. By creating a table, one can trace and record the values of variables at various steps to understand the program’s behavior or locate errors.

Comparison of actual with expected output: Analysing the output of code and comparing it to what was expected to identify discrepancies. If actual results differ from expected results, it suggests a problem that needs to be debugged.