Presentation is loading. Please wait.

Presentation is loading. Please wait.

Error Analysis Logic Errors.

Similar presentations


Presentation on theme: "Error Analysis Logic Errors."— Presentation transcript:

1 Error Analysis Logic Errors

2 Review So far we have discussed two categories of errors:
Syntax Errors: We see these at Design Time, before the program runs Runtime Errors: We see these at Runtime, as the program runs Today’s topic is the scariest category of error for the programmer because it may go unseen potentially forever.

3 Logic Errors A logic error is an error that prevents the program from doing what it is intended to do, producing unexpected results: This could be something nice and simple like adding when you meant to multiply while calculating the area of a rectangle Or something much more complicated like two objects passing through one another in a game that should have collided. The program still runs and finishes fine, but not with the results you were expecting.

4 Logic Errors These are the hardest errors to find and fix because the program does not actually break We do not necessarily know what the cause of the error is, and in some cases the error may not be even seen for the first time until years after the product has been in the consumer’s hands. These are the errors that will cause a programmer the most stress, yet are the ones every programmer feels most proud of when they solve.

5 Let’s Try an Easy One… double radius = 0; double area; area = 2 * Math.PI * radius; System.out.println(“The area is: “ + area); Wrong equation, PI * radius2

6 Remedy Similar to runtime errors, the only way to prevent logic errors is testing. However, when a logic error is found there unfortunately is no sure fire way to solve the problem. As said before, you may not even know where in your program the exists. Let’s take the two examples given in the beginning: In the first example, the logic error was adding when we should have multiplied the length and width to get the area. This is simple to solve because during the process of using the software people would quickly see that their area was always smaller than it should be. So the natural place to look would be the area calculation code

7 Remedy In the second example we stated that two objects moved through one another in a video game that should have collided. This is an extremely complicated issue and can be the result of any number of problems leading up to the test looking for a collision. In this case, as in most logic errors, it is best to start at the end and work backwards. Meaning, find the code that does the collision test and examine the values of variables and determine which, if any, are out of sorts. When this is done, find out why the data is bad working your way backwards to where that data was set, and so on.

8 The Debugger To help us in the process of debugging our programs we can force the Debug Perspective and use its many helpful tools. In the screen shot you can see the program in the Java Perspective. Beside one of the lines on the left you see a blue dot. This is called a break-point and it was manually placed by double-clicking in that blue bar.

9 The Break-Point A break-point is like a pause point for our program, when we run our program using F11 (or Bug button) it will execute as normal until the code gets to the break-point line and will pause execution AND change to the Debug Perspective. It is sitting on that line, but not executing it until we tell it to. This is the most valuable tool in debugging code. When the program is paused we can mouse-over any variable and see its current value. We can also start stepping through the code executing one line at a time.

10 Stepping Through the Code
We have the following controls while the program is paused to help us “step- through” our code one line at a time: Mouse over a variable to see its current value Select a variable OR expressionright-click itWatch: This will add it to the Expressions tab in the top right of the Debug perspective and will give you a constant watch of its value so you do not have mouse-over anymore. F6: Execute the current line of code fully F5: Go-into the next subprogram on the current line of code (only useful if it is your subprogram and not a prebuilt Java one) F8: Resume program as normal (this will pause again if it gets to another break-point)


Download ppt "Error Analysis Logic Errors."

Similar presentations


Ads by Google