﻿[Script Info] Title: [Events] Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text Dialogue: 0,0:00:00.57,0:00:06.31,Default,,0000,0000,0000,,Ã¯Â»Â¿Student W.Q. Feng asked when do you use a recursive definition versus a 'while' loop? Dialogue: 0,0:00:06.31,0:00:12.21,Default,,0000,0000,0000,,So this is a good question and in terms of being able to solve a problem, Dialogue: 0,0:00:12.21,0:00:15.95,Default,,0000,0000,0000,,anything you can do with recursion you can also do with a 'while' loop. Dialogue: 0,0:00:15.95,0:00:18.30,Default,,0000,0000,0000,,Anything you can do with a 'while' loop you could also do with recursion. Dialogue: 0,0:00:18.30,0:00:20.48,Default,,0000,0000,0000,,So any problem you can solve either way. Dialogue: 0,0:00:20.48,0:00:23.67,Default,,0000,0000,0000,,Usually solving it with a 'while' loop would be more efficient Dialogue: 0,0:00:23.67,0:00:26.68,Default,,0000,0000,0000,,because you donÃ¢ÂÂt have the overhead of all the extra procedure calls. Dialogue: 0,0:00:26.68,0:00:30.46,Default,,0000,0000,0000,,Often solving it with recursion is a simpler and more elegant solution. Dialogue: 0,0:00:30.46,0:00:34.76,Default,,0000,0000,0000,,So depending on whatÃ¢ÂÂs more important, oftentimes the tradeoff Dialogue: 0,0:00:34.76,0:00:38.13,Default,,0000,0000,0000,,is much better where the simpler elegant solution Dialogue: 0,0:00:38.13,0:00:41.05,Default,,0000,0000,0000,,that performance difference you probably couldnÃ¢ÂÂt even measure Dialogue: 0,0:00:41.05,0:00:43.78,Default,,0000,0000,0000,,unless you were timing really carefully for really large inputs, Dialogue: 0,0:00:43.78,0:00:45.25,Default,,0000,0000,0000,,you wouldnÃ¢ÂÂt notice the difference. Dialogue: 0,0:00:45.25,0:00:48.12,Default,,0000,0000,0000,,So itÃ¢ÂÂs more important to get the solution correct and have it Dialogue: 0,0:00:48.12,0:00:51.33,Default,,0000,0000,0000,,in code thatÃ¢ÂÂs easier for you to write and easier for other people to understand, Dialogue: 0,0:00:51.33,0:00:54.35,Default,,0000,0000,0000,,in many problems the recursive solution Dialogue: 0,0:00:54.35,0:00:57.86,Default,,0000,0000,0000,,really lends itself to that. ItÃ¢ÂÂs much shorter than the iterative solution would be Dialogue: 0,0:00:57.86,0:01:00.19,Default,,0000,0000,0000,,and itÃ¢ÂÂs easier to understand and follow. Dialogue: 0,0:01:00.19,0:01:04.33,Default,,0000,0000,0000,,In places where performance matters and we saw this with the Fibonacci example Dialogue: 0,0:01:04.33,0:01:07.50,Default,,0000,0000,0000,,that if you end up blowing up the number of calls in a way Dialogue: 0,0:01:07.50,0:01:09.60,Default,,0000,0000,0000,,that makes it too slow to do recursively, Dialogue: 0,0:01:09.60,0:01:13.37,Default,,0000,0000,0000,,you might prefer a more complicated solution that involves a 'while' loop and avoids the recursive calls.