Menu

using simple variable as "start" value in FOR

2018-02-06
2018-09-05
  • polyconnect

    polyconnect - 2018-02-06

    Trying to passing a value to a FOR statement with a simple byte variable and it doens't work.
    Am I doing it wrong ?
    The first FOR doens't work, the second one works obviously.

    ;Chip Settings
    
    #chip 18F47K40
    #config RSTOSC=HFINTOSC_64MHZ
    
    ;Defines (Constants)
    #define LCD_IO 8
    #define LCD_RS PORTA.3
    #define LCD_RW PORTA.5
    #define LCD_Enable PORTE.0
    #define LCD_DATA_PORT PORTD
    #define USART_BAUD_RATE 31250
    #define LCD_DB0 PORTD.0
    #define LCD_DB1 PORTD.1
    #define LCD_DB2 PORTD.2
    #define LCD_DB3 PORTD.3
    #define LCD_DB4 PORTD.4
    #define LCD_DB5 PORTD.5
    #define LCD_DB6 PORTD.6
    #define LCD_DB7 PORTD.7
    
    ;Variables
    Dim start_value As byte
    
    start_value = 10
    
    For counter = start_value to 0
        Locate 1, counter
        print ("1")     
    Next
    
    For counter = 10 to 0
        Locate 0, counter
        print ("1")     
    Next
    end
    
     
  • David Stephenson

    Your counter is going down so you need to add step -1 to the end of the line.

     
  • polyconnect

    polyconnect - 2018-02-06

    indeed ! I (naively) assumed that GCB would deduce the direction from the value it gets.
    thanks a lot

     
  • stan cartwright

    stan cartwright - 2018-02-06

    You don't need the minus sign but makes it more readable.

     
  • polyconnect

    polyconnect - 2018-09-05

    quick question : can we use a word as a counter variable ? I've tried but it seems it only counts to 255 and consider any higher value to be equal to 255

     
    • Anobium

      Anobium - 2018-09-05

      Declare a word, integer to long variable and then use this variable.

      dim myWordCounter as word
      for myWordCounter = 0 to 0xfff
              'do funcky stuff
      next
      
       

Log in to post a comment.