FOR Loop values
Does anyone know if the FOR loop valiable can be manipulated inside the loop? For example:
FOR (X=1;X<10,X++)
{
IF (some_variable == 1)
{
X=X-1
}
ELSE
{
send_command dvTP, "'@TXT',X,'some data'"
}
}
When I debug "X", the loop moves so fast that I can't see if it ever subtracts one (some_variable is always 1 at some point) and the results of the loop seem to indicate that x=x-1 doesn't work. Can I get x to change once inside the loop, independent of the counter?
Jeff Lockyear
Synergy Home Systems, Toronto
FOR (X=1;X<10,X++)
{
IF (some_variable == 1)
{
X=X-1
}
ELSE
{
send_command dvTP, "'@TXT',X,'some data'"
}
}
When I debug "X", the loop moves so fast that I can't see if it ever subtracts one (some_variable is always 1 at some point) and the results of the loop seem to indicate that x=x-1 doesn't work. Can I get x to change once inside the loop, independent of the counter?
Jeff Lockyear
Synergy Home Systems, Toronto
0
Comments
You'll have to be carefull when subtracting, though, so you don't create an endless loop.
--D
Jeff
your condition is that X has to be smaller than 10. When X is smaller than 10 it loops through again. On every loop through it raises x with 1 (x++)
but in your for loop, you subtract x with 1
Sounds like an endless loop to me...
You can manipulate variables inside a FOR loop but i have NO idea why you would do that.
And dchristo, i wonder what came out of your example. My guess is that it posts x = 1 x = 3 x = 5 etc, am i correct?
The reason Jeff can't see the value changing is because the loop is always starting over at 1.
Here are a few other loop ideas that might help.
Hope this helps!
Close... x=2, x=4, x=6, x=8, x=10
--D
offcourse
One fundamental programming practice says that you should code in a predictable and consistent manner in order to protect yourself (and the next guy) from misreading your code next time it is looked at.
There are many simple implications of this and one of them is most certainly that you shouldn't mess with the loop counter inside the loop, even though you can, without a very good reason.
The problem is that not every reported channel is valid, so I end up with blank sections of my list on the touchpanel. What I'd like to do is advance through the structure, but only send valid channels out. I was thinking that subtracting one from my loop would bring the counter back by one, thereby "deleting" a space. That was a problem for a lot of reasons! So I've abandoned that idea. Any suggestions would be greatly appreciated. I really have a mental block on this one.
Jeff
I'd use a WHILE instead of a FOR loop, since you don't know ahead of time exactly how many spaces ahead you're going to look. The following is pseudocode: Don't try to enter this directly into your program, I just wanted to illustrate the concept.
Edit: I've also realized there is a flaw with this algorithm in that you will not be able to move backwards past an invalid channel because it always searches forward for the next valid channel. So you'd need to set up another while loop to keep going back until you found a valid channel to start on.
Is it possible to qualify the channel before loading it into the structure thereby eliminating this issue before it occurs.
Jeff
My approach would be to not load the blank channels in to the structure. I'd add another slot in the structure and only store channel numbers that are valid. Why have blank slot?
If you are getting the data from some online, dynamic source, then there will not always be a channel for every number, and the channels which are valid channels may periodically change...
Jeff
So as most of your comments suggested, I was totally on the wrong track. Instead of manipulating the output to the touchpanels, I just ignored any null channels and didn't include them in my structure. Took me like five minutes and works perfectly.
Thanks for all your input!
I blame my formative years "programming" Landmark....
Jeff