FOR...NEXT strange counting


Closed Thread
Results 1 to 14 of 14

Hybrid View

  1. #1
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    938


    Did you find this post helpful? Yes | No

    Default

    Thank you keithdoxey,

    It is not about to find a solution to make it work; it is about how the FOR...NEXT loop counts.

    There are lots of differents solutions to solve my problem and I wonder how many people noticed this stange loop counting behaviour.
    Roger

  2. #2
    Join Date
    Feb 2003
    Posts
    432


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by flotulopex
    It is not about to find a solution to make it work; it is about how the FOR...NEXT loop counts.
    I think it is the same in any language.

    you specify the start and end points for the count and optionally the step size.

    the count starts at the specified start value then adds (or subtracts) the step value. It is then tested to see if it is within the range specified at which point the actions are executed. Once outside the specfied range it exits the for/next loop. This means that the value has to have changed from the last time the loop code was executed.
    Keith

    www.diyha.co.uk
    www.kat5.tv

  3. #3
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,116


    Did you find this post helpful? Yes | No

    Default

    Hi Flotul

    As I stated at #6 there is no strange behaviour. It is just the way for-next loops work in every version of Basic. Inside the loop the result is absolutely correct. Outside is expected to be +1 or -1 depending on the step of counting.

    I see nothing strange here.
    Other way to make a loop like While-Went or Repeat-Until use different counting methods.

    While-wend for example, first checks, then executes. For-Next, first executes then checks.

    Ioannis

  4. #4
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    OK i'll try another explanation
    Code:
        For Variable=0 to 255
            Pouet pouet
            next
    The test is done at the FOR TO NEXT line, the incrementation is done at NEXT line.
    So at the end the value=256... wich is really hard to fit in a BYTE variable. Hence it gives 0

    Change your var to a WORD too see what's happen now.

    Code:
        Repeat
            Pouet Pouet
            Variable = Variable +1
            Until Variable = 255
    a Repeat Untill loop do the test at the end... so this why it worked
    Last edited by mister_e; - 12th October 2006 at 08:25.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  5. #5
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    938


    Did you find this post helpful? Yes | No

    Default

    Thank you All,

    All your explanations are absolutely clear.
    Roger

  6. #6


    Did you find this post helpful? Yes | No

    Default

    Hi Flotulopex. The FOR - NEXT is confusing to me too. I use a different routine from a basic compiler I used a long time ago. This is a snip from a motor speed controller that controls the speed of an electric cart. At the start of the program, LEFTRAMP is made 0. If the speedpot is reduced while running, it will also ramp down.

    LEFTON: 'TURNS ON LEFT MOTOR FOR RIGHT TURN
    ADCIN 2, SPEEDPOT
    IF LEFTRAMP < SPEEDPOT Then LET LEFTRAMP = LEFTRAMP + 1
    IF LEFTRAMP > SPEEDPOT Then LET LEFTRAMP = LEFTRAMP - 1
    Pause 1 'CONTROLS THE RAMP-UP DELAY
    HPwm 1,LEFTRAMP,15000 'PWM OUTPUT DUTYCYCLE LIMITED TO SPEEDPOT
    GoTo LEFTON

Similar Threads

  1. COUNT is not counting again
    By jellis00 in forum mel PIC BASIC Pro
    Replies: 33
    Last Post: - 19th June 2009, 04:52
  2. Remain counting while sending out a pulse
    By ultiblade in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 10th January 2007, 15:51
  3. Strange behaviour from PIC16F877 on TMR0
    By mikebar in forum mel PIC BASIC Pro
    Replies: 18
    Last Post: - 19th August 2006, 01:31
  4. continious counting process (capture)
    By asynch in forum General
    Replies: 1
    Last Post: - 17th February 2006, 07:42
  5. Strange ASM file
    By barkerben in forum General
    Replies: 2
    Last Post: - 29th November 2004, 18:54

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts