1Repetition and Recursion9/26/20072Opening Discussion■Do you have any questions about the quiz?■Let's look at some solutions to the interclass problem.3Hierarchy of Bugs■When there is something wrong with a program we call it a bug. This is for historical reasons even though modern bugs are caused by the programmer, not chance.■There are three levels of bugs:Syntax errors – The simplest to fix. The compiler helps you.Runtime errors – Code compiles, but crashes when you run it. Tools can help you find the point where it crashes.Logic errors – Code compiles and runs, but gives you the wrong error. Hardest to find of the errors.4Using the Debugger■The compiler helps you with syntax errors, but you need a different tool to help you with runtime and logic errors.■Printing extra debug statements is the most general technique, but it is often faster to use a tool called a debugger.■On this system we have a debugger called gbd. If you compile your program with the -g option you can run your program in gdb and get extra information.■gdb has built in help, but to get you started use run to start the program, where to see a stack trace, and print to see variable values.5The Comma Operator■One of the more unusual operators in C is the comma operator.■It is the lowest priority operator and is just used to separate other expressions.■It has the value of the expression on the right side. So the left side has to have a side effect in order to do anything.■You will probably never see the comma operator used outside of a for loop even though to do so is legal.■Using commas in the wrong place in C can lead to very odd errors.6The Power of Recursion■We already saw how we can get repetition through recursion. Of course, C has loops for doing repetition and you should generally use them for basic repetition.■The real power of recursion comes from the fact that the stack remembers things for you. A loop doesn't.■This also allows recursive methods to call themselves more than once. A recursive method that calls itself more than once generally requires some thought to convert to a loop.■Let's see how we can use the memory of the stack.7Minute Essay■Why are logic errors so much harder to find than syntax or runtime errors?■Remember that assignment #3 is due Friday at midnight.■Interclass Problem – Do either problem 49 or 50 on page
View Full Document