Name _________________________

Teammate _________________________

CSC001- Worksheet 3: Errors and Debugging

ublkline620.GIF (83 bytes)

D. Joseph
Fall 1998

Due: Friday, November 13
Total Points: 10

This is a TEAM assignment! Each team member must turn in a worksheet.

ant_side.gif (7310 bytes)ant3.gif (8258 bytes) 

Introduction

One of the first computers, which was owned by the Navy, would not print some important data one day. After programmers spent many hours trying to find the solution within the program, a woman named Grace Hopper decided to check the computer. She found a small moth lodged between two wires. When she removed the moth, the computer began working perfectly (although the moth didn't have as much luck).

The late Grace Hopper retired from the Navy as a Rear Admiral. Although she was responsible for developing many important computer concepts (she was the author of the original COBOL programming language), she might be remembered best for discovering the first computer bug. Because an insect was discovered to be the culprit in the Navy computer, errors in computer programs are known as computer bugs.

When you test a program, you might have to debug it--remove the bugs, or errors--by correcting your typing errors and changing the logic so that the program does exactly what you want.

 

Definitions and Examples

As you know from the story above, looking for and fixing program errors, or bugs, is called debugging. Most debugging should take place while a program is still on paper--that is, before it is edited, interpreted, and run. Errors fall into three general categories. When you find an error in a progam it might be a syntax error, a run-time error, or a logic error.

Syntax Error

A syntax error is caused when an instruction does not follow the rules (or grammar) of a programming language. Syntax errors can be caused by misspelling a command word, or by using incorrect punctuation, such as a colon (:) when a semicolon (;) is required. Many of today's programming language development tools point out syntax errors as you type each line of code.

An example of a syntax error:

FR count = 1 TO 100

In this example, the command word FOR is misspelled and a syntax error occurs. If your program will not run (execute), it is probably because you have a syntax error.

Run-time Error

A run-time error is an error that shows up when you run a program. Errors that occur during the execution of a program are usually harder to detect than syntax errors. They are usually fatal and cause the program to stop and an error code or message is displayed. Run-time errors can be caused by logic errors or by other mistakes made when writing the program.

An example of a run-time error:

Result = (100 + 550 - 320) / 0

This example will cause a division by zero run-time error. If your program halts during execution, it is probably because you have a run-time error.

Logic Errors

A logic error is a special type of run-time error. A logic error is the result of a mistake made in logic when the
program was designed or written. Sometimes when there is a logic error the program will run perfectly, but produce meaningless results. Logic errors are hard to find and can be corrected only by thoroughly testing
the program.

An example of a logic error:

IF price1 < price2 THEN PRINT "Pizza 2 is the best deal for the money."

In this example a greater than symbol (>) should have been used. The user of the less than (<) symbol produced a meaningless result and caused a logic error.


The Problems

Examine the code segments below and determine the type of error(s) that would occur if you attempted to run the program. Note: It is not necessary to type in the code to determine the types of errors. By now, you should be able to recognize the errors on your own without the aid of a computer!

  1. PRNT "I cant't wait until Thanksgiving! I'm going to eat and eat and eat!"

    Type of error:_____________________________________________________________________


  2. week = 1
    WHILE week = 1
          PRINT "The semester is almost over! Whew!"
    WEND


    Type of error:_____________________________________________________________________


  3. totalAge = age1 + age2 + age3 + age4
    numAge = 4
    aveAge = numAge / totalAge


    Type of error:_____________________________________________________________________


  4. sum = 0
    FOR number = 10 TO 1 STEP 2
             sum = sum + number
    NEXT number


    Type of error:_____________________________________________________________________


  5. INPUT "Please enter your groups golf scores:"
    PRINT "First score ==>", score1
    PRINT "Second score ==>", score2
    PRINT "Third score ==>", score3
    PRINT "Fourth score ==>", score4
    TotalScore = score1 + score2 + score3 + score4


    Type of error:_____________________________________________________________________


  6. FOR count = 1 TO 5
             PRINT "This is line number: "; count


    Type of error:_____________________________________________________________________


  7. IF Grade >= 90 THEN PRINT "Yahoo! I got an A!"
    IF Grade <= 90 THEN PRINT "I didn't get an A."


    Type of error:_____________________________________________________________________


  8. LTE intern.name$ = Monica

    Type of error:_____________________________________________________________________


  9. PRINT I like CSC001 so much, I'm going to major in C.S.

    Type of error:_____________________________________________________________________


  10. times = 1
    WHILE times <= 7
            PRINT "Ha ";
            times = times + 1
    PRINT "The End"


    Type of error:_____________________________________________________________________
     

 

ublkline620.GIF (83 bytes)

Return to:  CSUS | Computer Science

October 28, 1998
Comments to:  josephd@csus.edu