0

So I've been wondering myself if it's better to use Recursion in Swift or the regular For Loops (iterations in general).

If so in which cases should I choose to use Recursion instead of Iterations.

I know that in some languages one is better/faster than the other, but I'm in doubt about Swift (maybe the same opinion as Objective-C).

If you can give me a solid, or close to, answer I'll be even more thankful.

Thanks in advance, and stay strong in code :) Cheers. - IC

Ivan Cantarino
  • 3,058
  • 4
  • 34
  • 73
  • Duplicate of http://stackoverflow.com/questions/72209/recursion-or-iteration really (the fact that it is Swift is irrelevant). Note that the Swift idiom for iteration is usually not usually `for` at all, but rather a sequence method such as `map` or `forEach`. And also keep in mind that C-style for loops will be removed from the language Real Soon Now. – matt Feb 15 '16 at 00:39

1 Answers1

1

This question is a bit general and has many answers already. Regardless, I would always stick with iteration when possible for readability's sake and the fact that it's more efficient than recursion. This is because we often end up solving the same math more than once in recursion. I've also worked with people who discourage the use of recursion in favor of iteration.

Cole
  • 2,641
  • 1
  • 16
  • 34