How does this loop get screwed up?
vegastech
Posts: 369
I have this for loop programmed into a button press:
LOCAL_VAR integer nChannel FOR (nChannel=0;nChannel<4;nChannel++)but it seems that when I press the button, the variable starts at 0, and then jumps to 4. Shouldn't it only increment by 1 after each press?
0
Comments
Your for loop isn't going to stop after one time through and wait for you to hit the button again. Its going to execute the loop until nChannel is not less than 4 every time you hit the button. What were you expecting to happen?
Paul
I think this should work.
Jeff
Jeff
Yeah, a FOR loop doesn't work that way in Netlinx. Once started it will run on its own without stopping. There's not really a 'Next' command as you might see in other programming languages.
But instead i just saw 0 -> 255
It'll break the FOR loop when it's encountered but it won't restart it.
--John
This is where a button_var (a variable only specific to a button event; broader scope than a local or stack, but smaller than a global) would come very handy. Unfortunately, in a circumstance like this we "waste" / unnecessarily use a global variable.
Anyway - I'd vouch for code like this.