Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 16 Online
OpenStudy (anonymous):

could someone help me with the concept behind iteration in python

OpenStudy (anonymous):

Iteration is the process of performing an action until a certain test is met. For example if you are driving to Grandma's house and she lives at exit 10 then you evaluate each exit to see if it is exit 10 if it not then you continue driving and test the next exit. Here is a quick example using a "while" statement. The interation occurs as I increase the exit until it equals 10 #Are we there yet/ exit = 5 #set starting exit while exit <> 10: #test for exit 10, while exit DOES NOT EQUAL 10 print "Only at exit ", exit, " keep on going" exit = exit +1 #drive to the next exit. #run test again #when test is true print "We are at exit ", exit, "I hope Grandma is home" #This prints when we get to exit 10 #end You can copy this program, save it and run it to see what it outputs./ The ouput should look like this: Only at exit 5 keep on going Only at exit 6 keep on going Only at exit 7 keep on going Only at exit 8 keep on going Only at exit 9 keep on going We are at exit 10 I hope Grandma is home

OpenStudy (anonymous):

So basically iterative is using the while sumthing sumthing: do this and recursive is for each of these: do this ? Or have I misunderstood?

OpenStudy (anonymous):

No wait, the recursive one modifies what we are working on right? So for problem set three it would change the target string...

OpenStudy (anonymous):

thank you both for the help

OpenStudy (anonymous):

No no no - I'm not helpful. I'm just trying to understand it too. After I posted this I found and read the Wikipedia entry on recursion: http://en.wikipedia.org/wiki/Recursion_%28computer_science%29 It helped me heaps, especially the bit about the Fibonacci function where it's written in both pseudocode and python. So for iterative code you use for: or while: but for recursive you call the function within the function using things like if: elif: and else:

OpenStudy (anonymous):

ok well the wikipedia info looks good and rwmuller did a great job

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!