Loop Examples 9 24 2007 1 Opening Discussion Let s look at some solutions to the interclass problem I ve added a new link on the links page for Project Euler This site has a bunch of programming problems that are mathematically interesting over a range of difficulties You know enough to do the easier ones now Departmental mailing lists If you are thinking of majoring or minoring you should consider putting yourself on them 2 Local static Variables I was recently reminded of an obscure topics I forgot to cover when we did functions local static variables The keyword static in C family languages means something akin to only one As you know normal local variables a created each time a method is called and destroyed when it returns so they have no memory of earlier calls Local static variables declared with the keyword static aren t like this They are like global variables with a local scope Only one of them is created no matter how many times the function is called and it remembers its value between calls 3 Nested Loops Loops are just statements and can be placed anywhere a statement goes This includes inside of other loops or control structures When one loop appears inside of another the result is that the body of the inner loop is executed a multiplicative number of times 4 Using Loops Last time we introduced loops in C and ran through the simplest example we could with all three loop types to give you a feel for them Today we want to write some more significant code that involves loops We can start by converting our calculator to a loop instead of using recursion Then we can write some standard types of math functions If we have time we could write a function that doesn t have loops but involves vector math We can also do some loops on input other than a menu 5 Minute Essay Which type of loop do you like best Why Interclass Problem Write a function double sumNums int howMany that reads the specified number of doubles and returns their sum Include a main to test that it works 6
View Full Document