Information Technology Topic
Programming Terms and Error Types
- Read notes
- Flashcards
- Quiz
- Confident
Study focus - 3 things
- Testing means running a program on chosen test data and comparing the actual results with the expected results. Debugging means finding and fixing the cause of any error. A dry run checks the logic by hand on paper
- There are three kinds of error: syntax (breaks the rules of the language), run-time (the program starts, then fails on a step that cannot be done) and logic (it runs to the end but the results are wrong)
- Only a syntax error is reported before the program runs. A logic error is never reported by the computer, so you must work out the expected results first and compare
Section 1Testing and debugging
A bug is an error in a program. Two jobs deal with bugs. Testing means running the program with chosen input values and checking whether the results are what they should be. Testing tells you that something is wrong. Debugging means finding the cause of an error and fixing it. Debugging tells you where and why. Here's the trick: testing finds the symptom and debugging cures it. After a fix, you test again, because a fix can cause a new error.
Section 2Test data and expected results
Test data is a set of input values chosen so you can check a program. For each value you also write down the expected result, worked out beforehand from the rules of the problem. Example: a pass is 50 marks or more. Test data 72, 50 and 49 should give Pass, Pass and Fail. Watch out: never work out the expected result by running the program. Then a bug would give you the 'right' answer too. Use the rules, not the program. Good test data includes ordinary values and values at the edge of a rule, such as exactly 50. That is where programs most often go wrong.
Section 3Dry run
A dry run means working through a program by hand, with test data, line by line, and writing down the value of each variable as it changes. No computer is needed. A trace table is the usual way to record it. Take this Pascal-style fragment: total := 0; for i := 1 to 3 do total := total + i * 2; Pass 1: i = 1, total = 2. Pass 2: i = 2, total = 6. Pass 3: i = 3, total = 12. The final value is 12. A dry run finds logic errors before the program is ever run, because you can see where the values stop agreeing with the expected results.
Section 4Syntax errors
A syntax error means the program breaks the rules of the language, like a sentence with broken grammar. The translator finds it while translating the program, and the program will not run until it is fixed. Pascal-style examples: A missing semicolon between two statements. total = price * qty; (= is a comparison. Assignment needs :=) if mark >= 50 writeln('Pass'); (the word then is missing) A semicolon just before else. Pascal does not allow one there. A name that was never declared, such as typing totl for total. The compiler rejects it before the program runs, and most teachers file it under syntax errors. The message may point to a line after the real mistake, because the translator only notices the problem when it cannot continue. Look at the line before as well.
Section 5Run-time errors
A run-time error happens while the program is running. The statement follows the rules of the language, so the translator accepts it. But when the computer tries to carry it out, the step is impossible and the program stops, usually with a message. Division by zero is the classic case: average := total / count; fails if count holds 0. Free Pascal reports it as run-time error 200. Another is using an index outside an array. With range checking on, if i holds 6 then marks[i] on an array declared as array[1..5] stops with run-time error 201. The program may work for most inputs and fail only for one particular value, which is why you test with awkward values as well as ordinary ones.
Section 6Logic errors
A logic error means the program is written correctly as far as the language is concerned, it runs to the end, and it shows no error message. But the results are wrong, because the steps do not do what the programmer meant. Example: average := a + b / 2; looks fine. But division happens before addition, so for 40 and 80 it stores 40 + 40 = 80, not the correct average of 60. Other typical causes are a loop that runs one time too few, and using > where >= is needed so a boundary value is treated wrongly. The computer cannot report a logic error, because it does not know what you intended. You find it by comparing the results with the expected results, or by a dry run. Logic errors are usually the hardest to find.
Section 7Telling the three apart
Ask three questions in order. Did the program get past translation? If the translator rejected it, the error is a syntax error. Did it start, and then stop with an error message? That is a run-time error. Did it finish and give the wrong answer with no message? That is a logic error. A program can have more than one kind. The translator reports the syntax errors first. Only when those are fixed does the program run, and then the run-time and logic errors show themselves.
Examples
A student writes three Pascal-style programs. Each has one fault. Program X contains the line: total = price * qty; Program Y contains the line: average := total / count; and the user enters 0 for count. Program Z contains the line: average := a + b / 2; and the test data is a = 40 and b = 80, where the correct average is 60. (a) State the type of error in each program. (b) State when each error is first noticed. (c) Calculate the value Program Z stores in average and explain why the computer gives no error message.
(a) Program X has a syntax error: = is used where the assignment operator := is needed. Program Y has a run-time error: the line follows the rules, but dividing by 0 is impossible. Program Z has a logic error. (b) X is noticed when the program is translated, so it never runs. Y is noticed while the program is running, when it reaches the division (Free Pascal reports run-time error 200). Z is noticed only when the result 80 is compared with the expected 60. (c) Division happens before addition, so b / 2 = 80 / 2 = 40, and a + 40 = 40 + 40 = 80. The statement is valid and every step can be carried out, so the computer has nothing to complain about. It cannot know that 60 was intended. The fix is average := (a + b) / 2;, which gives 120 / 2 = 60.
A trader in Montego Bay wants a Pascal-style program to total five sales. The test data is 120, 80, 150, 60 and 90, so the expected total is 500. program SalesTotal; var i : integer; price : integer; total : integer; begin total := 0; for i := 1 to 4 do begin readln(price); total := total + price; end; writeln(total); end. (a) Complete a dry run for the program as written, showing i, price and total on each pass. (b) State the output and compare it with the expected total. (c) Name the type of error and explain why the computer gives no message. (d) State the correction and the output when the program is run again with the same test data.
(a) Pass 1: i = 1, price = 120, total = 120. Pass 2: i = 2, price = 80, total = 200. Pass 3: i = 3, price = 150, total = 350. Pass 4: i = 4, price = 60, total = 410. The loop then stops. (b) The output is 410. The expected total is 500, so the result is 90 too low. That difference is exactly the fifth price, 90, which the program never read. (c) It is a logic error. The program is valid, every step can be carried out, and it ends normally. The computer cannot know that five prices were meant, so only the comparison with the expected result shows the fault. (d) Change the loop to for i := 1 to 5 do. The fifth pass adds 90 to 410, so the output is 500, which matches the expected total.
Now let's water it
Session runs notes to flashcards to quiz back-to-back.
Where this comes from
- CSEC Information Technology Syllabus (CXC 30/G/SYLL 08, revised 2000, 2008, 2013) · Caribbean Examinations Council (CXC) · Section 3, Specific Objective 4 · Syllabus scope
- The Python Tutorial (sections 3, 4, 8) and The Python Standard Library, Built-in Types (version 3.15 pages) · Python Software Foundation · Section 8, Errors and Exceptions (syntax errors, exceptions), checked against (https://docs.python.org/3/tutorial/errors.html) · Facts
- Free Pascal Reference guide v3.2.2 (16.1 Programs, 16.5 Blocks and the language pages in the facts sheet), RTL entries Write, Writeln, ReadLn, and User's guide 3.2 and Appendix D · Free Pascal team · Section D, Appendix D Run-time errors (200 division by zero, 201 range check error), checked against (https://www.freepascal.org/docs-html/user/userap4.html) · Facts
- Free Pascal Reference guide v3.2.2 (16.1 Programs, 16.5 Blocks and the language pages in the facts sheet), RTL entries Write, Writeln, ReadLn, and User's guide 3.2 and Appendix D · Free Pascal team · Section 12.8.1, Arithmetic operators (division by zero with / or mod), checked against (https://www.freepascal.org/docs-html/ref/refsu44.html) · Facts
- Free Pascal Reference guide v3.2.2 (16.1 Programs, 16.5 Blocks and the language pages in the facts sheet), RTL entries Write, Writeln, ReadLn, and User's guide 3.2 and Appendix D · Free Pascal team · Section 3.3.1, Static arrays (index outside the declared range), checked against (https://www.freepascal.org/docs-html/ref/refsu14.html) · Facts
- Free Pascal Reference guide v3.2.2 (16.1 Programs, 16.5 Blocks and the language pages in the facts sheet), RTL entries Write, Writeln, ReadLn, and User's guide 3.2 and Appendix D · Free Pascal team · Section 13.2.3, The if..then..else statement (no semicolon before else), checked against (https://www.freepascal.org/docs-html/ref/refsu57.html) · Facts
- Free Pascal Reference guide v3.2.2 (16.1 Programs, 16.5 Blocks and the language pages in the facts sheet), RTL entries Write, Writeln, ReadLn, and User's guide 3.2 and Appendix D · Free Pascal team · Section 13.1.1, Assignments (:= is the assignment operator), checked against (https://www.freepascal.org/docs-html/ref/refsu52.html) · Facts
- Navy Electricity and Electronics Training Series (NEETS), Module 22, Introduction to Digital Computers (NAVEDTRA 14194, September 1998) · US Navy (Naval Education and Training Professional Development and Technology Center) · Chapter 3, Debugging and Testing (pp. 3-20 to 3-21; bugs, debugging, desk-checking by hand, compiler error diagnostics, test data that gives known results; 1998 US Navy text) and Appendix I Glossary "Debugging", checked against (https://vtda.org/bits/other/US_Navy/NEETS/US%20Navy%20NEETS%20-%20Module%2022-Introduction%20to%20Digital%20Computers%20NAVEDTRA%2014194.pdf) · Facts