1Basic Loops9-21-20062Opening Discussion■What did we talk about last class?■Do you have any questions about the assignment?■Do you have any questions about the reading?3Repetition■Last time we decided that we wrote a little calculator and we ran into a problem when we wanted the calculator to do more than one calculation.■We got around this by having a function call itself. This is called recursion and we'll spend more time on it in the future.■Recursion is not the normal way of getting plain repetition in C. The more standard method is to use loops.4Loops in General■There are three general types of loops that occur in programming languages.Pre-test loopsPost-test loopsCounting loops■Loops also have something of a general structure that includes four elementsInitializeConditionLoop bodyIterate■The order and nature of these elements can vary. Forgetting one typically means that you have made a mistake.5Loops in C■What are the different loops that you have in the C language? What type is each one? When would you want to use each one?6Adding Loops to Code■Let's change our calculator so it uses a loop for repetition instead of recursion.■We could even write code that can “parse” prefix math expressions.■If we add a bit of logic to our ray tracing code we could make it do something interesting, like “drawing” our sphere.7Repetition through Recursion■As we saw last class, we can do repetition by having a function call itself. This is called recursion.■Recursion is an incredibly powerful tool. In C you don't normally use recursion when a loop would suffice, but there are many problems where recursion can easily do things that can't be done with loops without throwing in a lot of extra complexities.■The critical key for any recursion, is that there has to be a chance for the function to not call itself.8Minute Essay■Write a function that will sum and return the numbers read from standard input until a value of zero is read.■Remember to turn in assignment #2 by midnight tonight.■Quiz #3 will be at the beginning of next
View Full Document