Menu

Sanity check needed on For Next loops please!

Help
2020-03-30
2020-04-02
  • Julian Horn

    Julian Horn - 2020-03-30

    I am not seeing expected behaviour in a For Next loop with a step. Take the code below:

    Main:
    for FlashCount = 1 to 6 step 2
    Set LED1 on
    wait 500 ms
    Set LED1 off
    wait 500 ms
    next FlashCount
    set LED2 on
    wait 1 s
    Set LED2 off
    goto main

    I expect to see LED1 flash 3 times and LED2 flash once (using it to indicate start of next loop) But LED1 flashes 4 times per flash of LED2
    Changing values produces: No step = 6 flashes; Step 2 = 4 flashes, Step3 = 3 flashes
    I am using a 16F18877 dev board and latest GCB@SYN and a fresh install on Win10
    What am I missing here? I have reasonable experince in programming since 1970's but usually, unexpected behaviour, is me not understanding the finer points of a new language or dialect or just simple stupidity :)
    Have I been in isolation too long already??
    TIA
    Julian

     
  • Anobium

    Anobium - 2020-03-30

    @Julian. You have not been locked up for toooooo long.

    The FlashCount needs to be adapted. Consider the steps as defined 1-2-5-7 The step 6 is never encountered.

    So does for FlashCount = 1 to 5 step 2 work? or, consider using Repeat 3 - End Repeat.

    :-)

     
  • Julian Horn

    Julian Horn - 2020-03-30

    Thanks - yes I did try that and it does work.
    I guess the 'next' test is looking for an 'equal to' rather than 'equal to' or 'greater than'?
    In most Basics I have played with, I think => is the norm but thats fine, if I understand the issue I can work around it when needed.
    Appreciate the reply.

     
    • Anobium

      Anobium - 2020-03-30

      Look at REPEAT and SELECT CASE.

      :-)

       
  • stan cartwright

    stan cartwright - 2020-04-01

    For next with step is ok as you say Anobium if used correctly.
    I don't like using case , to me it slows code. I don't know. Like using and to test multiple variables.
    Is repeat until var=x more efficient than for next? or do loop until var=x

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.