Questions about Javascript looping

Asked

Viewed 75 times

1

I noticed it’s used a lot var i and when is for/in and var x, why use these letters?

I can use words in their place and it will work normally.

I can put a for into another? That would cause some trouble?

When while shall be used and the difference between while and do/while?

I was trying to understand a little bit about it by reading on w3schools, but it’s not very clear.

1 answer

3


There’s no pattern and I never noticed this variation of i for x because of this, some people like i (I), some people like x (I think it’s for something else), and of course there are those who must have adopted the pattern of one in a kind of loop and another in the other type. I see no reason for it, who does it should explain why it does, but I already kick that it is just like.

You can use whatever you want, there is only one convention to use a simple letter because in general it is only there because of the mechanism of looping and is not part of the domain of the problem, so it is up to a way to differentiate and avoid emphasizing its presence. You can use contador as some do, find it horrible and I see it as a decrease in readability, bringing verbosity to something that doesn’t matter.

Even better when you can use a loop construction that doesn’t need a control variable and it already sets a variable for the item being analyzed in that step.

You can make a for inside the other and can use i, j, k, l (though it may be confused with 1 (one), or other letters. In some scenarios it may be that the x, y, w, z are more suitable if you are talking about axes, as there are cases for use of mand n, or a, b, c, d, e, but are more rare. i and j are the most common fired in codes. "Everyone" knows what is.

The only problem of a for inside the other is that you have an exponential algorithm (complexity represented by O(N2)) and this is not very desirable, but it is not so tragic and there are cases that is the only way to do it. And if it’s at low volumes it shouldn’t make so much difference. Just make sure you really need to do it this way.

About the while has already been answered: What’s the difference between while, for, while and foreach? and What is the usefulness and importance of "do... while"?. I don’t know if it’s true yet, but they say W3scools is not a good source.

  • Speaking for myself only, I use only because where I learned it was written i, and in most places too. I believe that if you use i because it’s kind of in the middle of the alphabet, and since it’s common to use either 'a, b, c' or 'x, y, z', some convention was done a long time ago to use i, j, k for loops not conflicting with other variables that will appear. But that’s just a kick.

  • 2

    I think, and I just think, that `i is from index. Then it was natural to continue. I think it comes from mathematics itself. In general they are the only variables of a single letter that they should have in the code, unless actually something significant has only one letter.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.