Download presentation
Presentation is loading. Please wait.
Published byAndra Gordon Modified over 9 years ago
1
1 CS 106 Computing Fundamentals II Chapter 211 “Coding And Debugging” Herbert G. Mayer, PSU CS status 6/29/2013 Initial content copied verbatim from CS 106 material developed by CS professors: Cynthia Brown & Robert Martin
2
2 Syllabus Requirements and Specification Requirements and Specification Bugs Bugs Types of Errors Types of Errors Stepping Through Stepping Through Single Step Through Code on Mac Single Step Through Code on Mac Compiling vs. Interpreting Compiling vs. Interpreting
3
3 Requirements and Specification Recall the four steps of problem solving: Orient, Plan, Execute, TestRecall the four steps of problem solving: Orient, Plan, Execute, Test Before you start the implementation phase of your project (the Execute step), complete the prior two stepsBefore you start the implementation phase of your project (the Execute step), complete the prior two steps By creating a requirements and specification documentBy creating a requirements and specification document
4
4 Bugs Errors in a program are called “bugs”Errors in a program are called “bugs” Don’t feel that your program should work right the first time. Almost every program starts out with bugs, even when written by expertsDon’t feel that your program should work right the first time. Almost every program starts out with bugs, even when written by experts The process of finding and correcting the errors is called “debugging”The process of finding and correcting the errors is called “debugging”
5
5 Three Types of Errors 1 Syntax error: (VBA calls these compiler errors) This is a mistake in the form of your program. The VBA editor will catch these and turn the offending line red. It will also suggest ways to fix it. Usually the suggestions are right but they may not be.Syntax error: (VBA calls these compiler errors) This is a mistake in the form of your program. The VBA editor will catch these and turn the offending line red. It will also suggest ways to fix it. Usually the suggestions are right but they may not be. Run-time error: This causes your program to freeze up or crash while running. A well-written program shouldn’t do this, even if the user does something wrong. We’ll learn some methods to avoid these later.Run-time error: This causes your program to freeze up or crash while running. A well-written program shouldn’t do this, even if the user does something wrong. We’ll learn some methods to avoid these later.
6
6 Three Types of Errors (2) Logic errors: These are the most insidious errors, because it looks like the program is running correctly.Logic errors: These are the most insidious errors, because it looks like the program is running correctly. Remember that computer can’t think; they only think they can.Remember that computer can’t think; they only think they can. A computer just does what you tell it to, no matter how silly. You use tests to see if you have logic errors, which is why the tests are of critical importance.A computer just does what you tell it to, no matter how silly. You use tests to see if you have logic errors, which is why the tests are of critical importance.
7
7 Stepping Through VBA has a great feature to let you see your program in actionVBA has a great feature to let you see your program in action If you go to the code page, choose a subroutine to run (by clicking the title) and choose Step Into from the Debug menu (or use the F8 key on Windows, command-shift-I on the Mac; that’s the letter “i”), the computer will execute your program line by lineIf you go to the code page, choose a subroutine to run (by clicking the title) and choose Step Into from the Debug menu (or use the F8 key on Windows, command-shift-I on the Mac; that’s the letter “i”), the computer will execute your program line by line The next line to be executed is highlighted in yellowThe next line to be executed is highlighted in yellow Keep pressing F8 or command-shift-I to advance through the codeKeep pressing F8 or command-shift-I to advance through the code After a line has executed, you can hover the mouse over the variable names and see their current valuesAfter a line has executed, you can hover the mouse over the variable names and see their current values If you see a value you didn’t expect, you have found a bugIf you see a value you didn’t expect, you have found a bug Go to Run menu and click Reset to change your code, try againGo to Run menu and click Reset to change your code, try again
8
8 Single Step Through Code on Mac Situation: your Excel workbook is open, with Macros enabled, you are in Developer mode, and have 1 macro or more, clicking EditorSituation: your Excel workbook is open, with Macros enabled, you are in Developer mode, and have 1 macro or more, clicking Editor This will show the VB source code on Module 1This will show the VB source code on Module 1 Select the sub you wish to debug, i.e. you are setting a break pointSelect the sub you wish to debug, i.e. you are setting a break point Click on debugClick on debug Now you can step through the code with “shift command i” instructionsNow you can step through the code with “shift command i” instructions On Microsoft this is accomplished with the more practical push of the F8 buttonOn Microsoft this is accomplished with the more practical push of the F8 button
9
9 Single Step Through Code on Mac
10
10 Single Step Through Code on Mac Option Explicit Sub debug_demo() ' debug_demo Macro Range("B2").Select Range("B2").Select ActiveCell.FormulaR1C1 = "100" ActiveCell.FormulaR1C1 = "100" Range("B3").Select Range("B3").Select ActiveCell.FormulaR1C1 = "200" ActiveCell.FormulaR1C1 = "200" Range("B4").Select Range("B4").Select ActiveCell.FormulaR1C1 = "quotient:" ActiveCell.FormulaR1C1 = "quotient:" Range("C4").Select Range("C4").Select ActiveCell.FormulaR1C1 = "=R[-2]C[-1]/R[-1]C[-1]" ActiveCell.FormulaR1C1 = "=R[-2]C[-1]/R[-1]C[-1]" Range("C5").Select Range("C5").Select End Sub
11
11 Compiling vs. Interpreting When you step through your code, VBA looks at it and executes it line by line. This is called interpreting the code.When you step through your code, VBA looks at it and executes it line by line. This is called interpreting the code. Normally when you run your program, VBA compiles it, meaning it is translated into a form that runs quickly on your machine, similar to a.exe file on WindowsNormally when you run your program, VBA compiles it, meaning it is translated into a form that runs quickly on your machine, similar to a.exe file on Windows
12
12 Be Persistent Programming requires you to pay attention to detailProgramming requires you to pay attention to detail Your program won’t run till every last syntax error is fixedYour program won’t run till every last syntax error is fixed Once it runs, you need to do your tests and track down any bugsOnce it runs, you need to do your tests and track down any bugs The TA can help you if you get stuck. Or use the discussion groups on D2LThe TA can help you if you get stuck. Or use the discussion groups on D2L
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.