When you’re *iterating* through something, you’re going through a loop until some condition is met. You see this with loops like:
for(int i = 0; i < 10; i++)
or
while(true)
or
while(isFinished == false)
or
List<string> strings;
foreach(string s in strings)
If you’re doing any of the .net languages, you’ll also see something like an IIterable interface that defines what you need to make something iterable. Essentially, it’s just looping through elements of some collection, or checking against a certain condition before running again.
Recursion is a little different. Recursion is when something calls itself.
A good example of that would be traversing a directory. You could set up a function to copy all files in a given folder and then move to the first folder in that folder, and you’d call the original function on that folder too.
So something like:
public void traverseDirectory(string path){
//TODO: copy files from this folder to another folder
foreach(string d in Directory.GetDirectories(path)){
traverseDirectory(d);
}
}
As you can see, you’re both iterating there (that foreach loop), but also doing recursion, because traverseDirectory is calling itself.
Latest Answers