View Full Version : 2 increments in For loop
can you increment 2 different variables in a loop? for example can you do something like this:
for (i=0,j=0;i<somethings.length;i--,j++) {
?
I thought i had seen something like this a while back, I may have been mistaken though..
jscheuer1
03-20-2006, 04:27 AM
Sure but, the way you have it, it will go on forever, (don't) try:
<script type="text/javascript">
for (i=0,j=0;i<10;i--,j++)
alert(i+' '+j)
</script>
With the above you will need task manager or something to get out of it. This, however will work fine:
<script type="text/javascript">;
for (i=0,j=0;i>-10;i--,j++)
alert(i+' '+j)
</script>
lol, oh right I didnt notice that until after I posted it..
I figured what I was doing wrong.. i was using it with onload=function() {
and i just realized that for loops run without the onload statement so I was creating a weird infinite loop aside from the other infinite loop:
i=0,j=0;i<10;i--,j++
lol.
anyway, thanks for your help.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.