Change variable increment/decrement into mid-loop?


+ Reply to Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2013
    Posts
    628

    Default Change variable increment/decrement into mid-loop?

    Hello.
    There is a some loop, which monitors keypress, updates display, etc.
    Also in loop is variable, which should increase from 1 to 100 and then decrease from 100 to 1.
    Is there any way to do it in simple way? without splitting loop into two parts?

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,331

    Default Re: Change variable increment/decrement into mid-loop?

    Something like this perhaps?
    Code:
    Cnt VAR BYTE
    Dir VAR BIT
    
    UP CON 1
    DOWN CON 0
    
    SomeLoop:
      ' Monitor keypress
      ' Update Display
      ' etc
    
      IF Dir = UP THEN
        Cnt = Cnt + 1
      ELSE
        Cnt = Cnt - 1
      ENDIF
    
      IF (Cnt = 100) OR (Cnt = 0) THEN
        Dir = ~Dir    ' Invert direction
      ENDIF
    
      Goto SomeLoop

  3. #3
    Join Date
    Feb 2013
    Posts
    628

    Default Re: Change variable increment/decrement into mid-loop?

    Yes, thanks!
    On spectrum, I was doing this by changing the sign of increment variable, but since in PBP we have no negative numbers....

  4. #4
    Join Date
    Feb 2013
    Posts
    628

    Default Re: Change variable increment/decrement into mid-loop?

    And yes, CNT=1 should be added in the beginning, or absolutely unexpected behavior will be generated (just tested)

  5. #5
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,331

    Default Re: Change variable increment/decrement into mid-loop?

    Regarding negative numbers, as long as both variables are of the same size what you did will work:
    Code:
    Cnt VAR BYTE
    Add VAR BYTE
    
    Cnt = 100
    Add = -1
    
    Cnt = Cnt + Add   ' Cnt now = 99
    But if Cnt was a WORD and Add a BYTE then Cnt woul equal 354.

Similar Threads

  1. Replies: 27
    Last Post: - 9th April 2015, 04:51
  2. Can not change variable while using DT_INT?
    By hvguy0 in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 29th March 2013, 23:05
  3. Altering a variable in a loop.
    By jmgelba in forum mel PIC BASIC Pro
    Replies: 26
    Last Post: - 18th September 2012, 06:24
  4. Change a variable to the opposite?
    By Hylan in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 21st June 2012, 07:00
  5. Let a variable change from bin to dec.
    By Roy in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 25th May 2004, 19:57

Posting Permissions

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