Home AMX User Forum NetLinx Studio

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

Comments

  • dchristodchristo Posts: 177
    I did a quick test with the following, and it worked:
    For(x = 1; x <= 10; x++)
    {
       x++
       Send_String 0,"'x = ',Itoa(x)"
    }
    

    You'll have to be carefull when subtracting, though, so you don't create an endless loop.

    --D
  • Good point about subtracting. Good to know it can be done though. I'll play with it a bit more and see what I can do. Thanks much.

    Jeff
  • yuriyuri Posts: 861
    i think you are missing the complete point about FOR loops.

    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?
  • GSLogicGSLogic Posts: 562
    Yuri is right, it will loop forever. The loop X value needs to reach 10 to stop the loop.
    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.
    for(x=1, y=10, z=2;  x<=10;  x++, y--, z=z+2) 
    {
       // x must go to 10 before loop will stop
       // y is counting backwards from 10 to 1
       // z will count by 2s, from 2 to 20
    }
    
    for(x=1, y=10;  x<=10;  x++, y--) 
    {
       if(x == 5)
          break;   // this is will stop the loop at 5
    }
    

    Hope this helps!
  • dchristodchristo Posts: 177
    yuri wrote:
    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?

    Close... x=2, x=4, x=6, x=8, x=10

    --D
  • yuriyuri Posts: 861
    dchristo wrote:
    Close... x=2, x=4, x=6, x=8, x=10

    --D

    offcourse :)
  • Structured programming

    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.
  • I think I opened a can of worms with my question. It seems I was on the wrong track anyway, although I haven't sorted out how I can do what I want to yet. Basically I have a Sirius receiver, and I'm populating a structure with all the channel numbers, names and info as reported from the reciever. I created a five line display on my touchpanels that allows me to scroll up and down through the list and then select a channel. Each push of the up or down button starts a FOR loop that advances through the structure and sends the next or previous five values out to the touchpanel. It works well, but for one problem.

    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
  • mpullinmpullin Posts: 949
    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.

    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:
    current_channel = lowest_channel - 1; // or lowest_channel + 1 depending on the direction
    current_slot = 1;
    while(current_slot < 6 && current_channel <= maximum_channel){
         if(channel_array[current_channel].is_a_valid_channel){
              touchpanel_field[current_slot].print(channel_array[current_channel]);
              if(current_slot == 1) lowest_channel = current_channel;
              current_slot++;
         }
         current_channel++;
    }
    
    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.
  • viningvining Posts: 4,368
    Jeff Lockyear wrote:
    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.

    Is it possible to qualify the channel before loading it into the structure thereby eliminating this issue before it occurs.
  • GSLogicGSLogic Posts: 562
    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. Jeff

    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?
  • mpullinmpullin Posts: 949
    A couple people are advising that the structure only store valid channels... which brings to question, where exactly are you getting the data that this structure holds?

    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...
  • Hey Matt, thanks for the bit of code. I have a good protion of it already, so I think you've put me on the right track. As for where I get the code, I know that the receiver has the capacity to accommodate 223 channels. Updating the channel list means querying every channel value, and either populating the structure with the variables (channel name, etc) or filling them with zeroes if I see teh invalid channel error. Ah. So maybe I need to go back during the query if I get an error, rather than filling the structure and THEN trying to get rid of it? That sounds like it might make more sense.

    Jeff
  • yuriyuri Posts: 861
    can't you just add an element to your structure that enables/disables it? And then while filling the structure toggle it on / off...
  • The sound of one hand clapping my forehead.

    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
Sign In or Register to comment.