3

I am doing Cs50 Harvard University online and I'm on week 3 but while watching the video I noticed that I iterations and loops seem the same, as they repeat things over and over. But there must be a difference or else they wouldn't have two names for the same thing. No matter how many times I re-watch the video I can't find a difference. Please help me understand.

  • 3
    Iteration means "to repeat". A loop is one structure for repeating things, but there are others. Usually they are the same. – stark Jul 12 '20 at 14:50
  • 1
    Loop is a language construct allowing executing some code many times. Iteration is the actual execution of this code. – 0___________ Jul 12 '20 at 14:50
  • "*No matter how many times I re-watch the video I can't find a difference.*" - Maybe change the source of knowledge then. The video might not has so high-quality. – RobertS supports Monica Cellio Jul 12 '20 at 15:29

3 Answers3

6

"Loop" refers to language constructs that are used to repeatedly execute some code. (for loops, while loops, etc.)

"Loop" can also refer to code being executed repeatedly. (e.g. "It's stuck in a loop.")

Iterating is the process of doing something repeatedly. (e.g. "This loop iterates over the elements of the array.")

An iteration is a single pass of a loop. (e.g. "In the first iteration of that for loop, i will be 0.")

ikegami
  • 367,544
  • 15
  • 269
  • 518
  • I personally would say that the first execution of the loop is not the first iteration. The first iteration is when the first repetition takes place. The word "iterate" is equal to "repeat" if you compare it at a terminology kind of base. – RobertS supports Monica Cellio Jul 12 '20 at 15:44
  • @RobertS supports Monica Cellio, That's not how it's used in CS. We say "each iteration", and that includes the first pass. – ikegami Jul 12 '20 at 15:49
  • Just because it is common used to say it that way (perhaps for the sake of simplicity at specific uses), doesn't mean it is strictly correct. An *iteration* is a *repetition* if one looks into the dictionary. – RobertS supports Monica Cellio Jul 12 '20 at 15:51
  • 3
    I don't think including an uncommon definition of the term is appropriate. /// The definitions I posted are the CS definitions. not the English ones. Note that every single use of "iteration" in [Loop (Computer) on Wikiedia](https://en.wikipedia.org/wiki/Loop_(computing)) refers to a loop pass. – ikegami Jul 12 '20 at 15:53
  • It is more appropriate in my eyes to stop using incorrect terms. One could argue about a 0th iteration, if you like to, but the first iteration is the first repetition. – RobertS supports Monica Cellio Jul 12 '20 at 15:57
  • Re "*It is more appropriate in my eyes to stop using incorrect terms*", "Iteration" is a proper term to use, and I posted the correct definition of it for the domain in question. – ikegami Jul 12 '20 at 15:59
  • No one really standardized these terms exactly. Wikipedia isn't a standard. Popularity doesn't mean it is a standard, too. To say that I'm incorrect and the common use is correct is not correct. Only if you can give proof of the opposite, I will be silent. – RobertS supports Monica Cellio Jul 12 '20 at 16:08
  • 1
    @RobertS supports Monica Cellio, When it comes to language, in the absence of standards (and even in the presence of them), the commonly accepted definition is indeed the correct one. (I used the Wikipedia page as an example, not a standard.) – ikegami Jul 12 '20 at 16:09
  • You've made your argument. I soundly reject it. There should be no more on this here. – ikegami Jul 12 '20 at 16:11
0

Usually "loop" refers to the code and "iteration" refers to the conceptual process of repeating some steps. In this case, they are more or less the same. Additionally, you could use "iteration" to refer to a single repetition within a loop which gives it a different meaning.

A case example for the first usage of "iteration" would be:

You can print a linked list either by using iteration or recursion. If you use iteration, simply create a while-loop that propagates a "current" node through the list and stops when it becomes NULL.

And an example refering to specific repetitions:

The following loop outputs numbers 1 to 10 in reverse order. At the k-th iteration, 10-k+1 is printed:

for (i=10; i>=1; i--){
    printf("%d\n", i);
}
kyriakosSt
  • 1,754
  • 2
  • 15
  • 33
0

That's how I would define it:

"Loop" - A control flow statement that iterates the code in the loop's body dependent upon a provided loop condition. It is a language construct and in C we have three kinds of these constructs: for, while and do-while.

"Iteration" - One specific walkthrough of the code inside of the loop's body after the first walkthrough. In other words, a single repetition of the execution of the loop body's code.

When doing looping/iterating (as verb they can be seen equal indeed), you repeat over the code in the loop's body. Every single repetition is an iteration.

A loop's purpose is the capability to do iterations. Without being able to do iterations the loop construct is useless.

So, both terms are closely related, but not the same.

  • The downvote(s) is/are based upon the premise, that people think my definition of *iteration* in the sense of *repetition* is incorrect. I would say them are incorrect to call a non-repetition an iteration and to say that I'm wrong. An iteration is a repetition if you look in a dictionary. If anyone can give me proof of any official standard that "iteration" doesn't mean "repetition" and with that contrary to its generic meaning, I will change my answer. Otherwise I don't consider this definition as wrong and I only can hope you don't do so too. Always open for constructive feedback. – RobertS supports Monica Cellio Jul 12 '20 at 16:18
  • 1
    C 2018 footnote 161: “the controlling expression… specifies an evaluation made before each iteration… and expression-3 specifies an operation… that is performed after each iteration.” Java Language Specification SE 11: “Next [after initialization of a `for` statement], a `for` iteration step is performed…” [Uses of “first iteration” on Stack Overflow.](https://stackoverflow.com/search?q=%5Bc%5D+%22first+iteration%22) Do not use general language dictionaries for computer science terms. Technical terms might originate from general language but are given different meanings. – Eric Postpischil Jul 12 '20 at 18:34