Posts ID:168437 Favorites Creations
ID:168437

 ```mob/verb/Test() for(var/x=1, x<=5, x++) x-=2 ```

That gives me an infinite loop, which it obviously should.

 ```mob/verb/Test() for(var/x=1 to 5) x-=2 ```

That doesn't give me an infinite loop. Once the loop completes once, x is restored to whatever it should be rather than taking in account the x-=2.

I somehow doubt this is a bug, but can someone explain why this happens? It just caused me 2 hours of pain.
 #1 Oct 17 2005, 9:47 pm Ok..... Well it isnt a bug. Well basically this is what is happening. You define x and set it to 1. Then if it is smaller than 5 (which it is) it exucutes the statement which is taking 2 away from it. So now x is -1. Then it runs x++ which adds 1 to x which will equal 0. So x will never be 5 or over as you are taking 1 away every loop. ADT_CLONE
 #2 Oct 17 2005, 9:50 pm In response to ADT_CLONE (#1) I think you missed the entire point of my post. for(var/x=1, x<=5, x++) - Gives an infinite loop. for(var/x=1 to 5) - Doesn't give an infinite loop.
 #3 Oct 18 2005, 1:15 am In response to DeathAwaitsU (#2) I didnt even know for(var/x=1 to 5) or whatever you put was valid. I dont know what it does.
 #4 Oct 18 2005, 1:36 am In response to ADT_CLONE (#3) the reson is you can not modify the value x perintly. Test() for(var/x=1 to 5) x-=2 so first line for(var/x=1 to 5) x=1 next line x will = -1 then gos back to firs line and rembers that x was 1 before and so makes it 2 every thim this loops you loos your changes to the variable
 #5 Oct 18 2005, 2:05 am In response to The Riddler (#4) I know that it does that (After about 2 hours of frustration) but what puzzles me is why you can modify x in for(var/x=1,x<=5,x++) but not in for(var/x=1 to 5)?
 #6 Oct 18 2005, 2:58 am In response to DeathAwaitsU (#5) Because the first loop is more "manual" and directly modifies the vars. The second loop is a bit more like looping through a list - like for(var/x in list(1,2,3,4,5)) - in which case the current value of x doesn't affect which cycle the loop is up to.
DeathAwaitsU wrote:
I somehow doubt this is a bug, but can someone explain why this happens? It just caused me 2 hours of pain.

Yes, I can explain it. The syntax for(x=1 to 5) is equivalent in BYOND to this:
 ```for(x in list(1,2,3,4,5)) ```

x is always being set to the next item in the list regardless of its present value.

Lummox JR