It does not have a pattern and I've never noticed this variation from i
to x
because of that, there are some like i
(me), some like x
(I think it's for something else) , and of course there are those who must have adopted the one-in-one type of loop and another in the other type. I do not see a reason for this, who does it is that I should explain why it does, but I already kick that it's just like it.
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 looping mechanism and is not part of the problem domain, so it is up to a way of differentiating and avoiding emphasizing their presence. You can use contador
as some do, they look horrible and I see it as a decrease in readability, bringing verbosity to something that does not matter.
Even better when you can use a loop construct that does not need a control variable and it already sets a variable for the item being parsed at that step.
You can do for
within the other and you can use i
, j
, k
, l
(although it can be counted with 1 (one), or other letters. scenarios may be that x
, y
, w
, z
are more appropriate if you are talking about axes, just as there are cases for m
and n
, or a
, b
, c
, d
, e
, but are rarer. i
and j
are triggered the most common in codes.
The only problem with a for
within the other is that you get to have an exponential algorithm (complexity represented by O (N 2 )) 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 in low volumes it should not make that much difference. Just be sure you really have to do it this way.
About while
has already been answered: What is the difference between the while, for, while and foreach? and What is the usefulness and importance of "do ... while"? . I do not know if it's still true, but they say W3Scools is not a good source.