Announcement

Collapse
No announcement yet.

Loop Limit

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Loop Limit

    I've wondered whether a function used as the upper limit of a loop is computed once, or on every loop.

    Code:
    For i = 1 to ParseCount(Msg$,$CRLF)   'does this computer each loop
    
    vs...
    
    iCount = ParseCount(Msg$,$CRLF)
    For i = 1 to iCount

  • #2
    Funny how just asking a question makes you work out the answer to the question, to make sure someone doesn't call you a dufus for not figuring it out yourself!

    This example says the function is called only once ...

    Code:
    #Include "win32api.inc"
    Function PBMain() As Long
       Local i As Long
       For i = 1 To L
          Sleep 2000
          WinBeep(250,300)
       Next
    End Function
    
    Function L() As Long
       Beep : Function = 5   'I hear the Beep only 1 time
    End Function
    I don't remember seeing this documented.

    Comment


    • #3
      The instruction -

      stop is a numeric expression giving the value that Counter must reach for the loop to be terminated.

      Although not explicitly stated, I assume it to be only loaded once

      Comment


      • #4
        The counter is set up on the first iteration, so the function is called only once.

        You would not expect

        FOR X = 1 TO 10

        ...to reset to 10 after every loop so

        FOR X = 1 TO PARSECOUNT

        won't to do it either.
        "Not my circus, not my monkeys."

        Comment


        • #5
          Gary, I think you already have the correct answer but no demonstration. I modified the original string inside the loop. If the function were called each time, it would have displayed five values, instead it displays only the original four values as identified during the loop setup..

          Code:
               SUB test_loop_funct _
             ALIAS "test_loop_funct" _
                 ( _
                 ) _
                   EXPORT
          
          
            ' local storage
            local i as long
            local ierr as long
          
            ' String to test
            local mystring as string
          
          '-------------------------------------------------------------------------------
          program_begin:
             ierr = 0 ' No error
          
             ' Initialize String
             mystring = "A,B,C,D"
             ' Loop through using parsecount
             for i = 1 to parsecount(mystring, ",")
               ? "i="+str$(i) + " is " + parse$(mystring, ",", i)
               if i = 1 then
                  ? "Reset String"
                  mystring = "A,B,C,D,E"
               end if
             next i
          
             ' Recalc parsecount
             ? "After Loop: ParseCount="+str$(parsecount(mystring, ","))
          
          
             ' Done
             goto program_exit
          
          '-------------------------------------------------------------------------------
          program_exit:
          
          
             EXIT SUB
          
          END SUB

          Comment


          • #6
            Originally posted by Eric Pearson View Post
            The counter is set up on the first iteration, so the function is called only once.

            You would not expect

            FOR X = 1 TO 10

            ...to reset to 10 after every loop so

            FOR X = 1 TO PARSECOUNT

            won't to do it either.
            Easily proved!
            Code:
            #COMPILE EXE
            #DIM ALL
            
            FUNCTION PBMAIN () AS LONG
                LOCAL iterations AS LONG
                FOR iterations = 0 TO SetLimit()
                    SLEEP 1
                NEXT
                ? STR$(SetLimit(1))
            END FUNCTION
            
            FUNCTION SetLimit(OPTIONAL x AS LONG) AS LONG
                STATIC hits AS LONG
                IF ISMISSING(x) THEN
                    INCR hits
                    FUNCTION = 10
                    EXIT FUNCTION
                END IF
                FUNCTION = hits
            END FUNCTION
            The world is strange and wonderful.*
            I reserve the right to be horrifically wrong.
            Please maintain a safe following distance.
            *wonderful sold separately.

            Comment


            • #7
              Code:
              FUNCTION PBMAIN () AS LONG
              
               LOCAL x       AS LONG
               LOCAL counter AS LONG
               LOCAL s       AS STRING
              
               s = "1"
               FOR x = 1 TO VAL(s) 'evaluates VAL(s) once
                s = "9"            'see if we loop 9 times
                INCR counter       'Increment loop counter
               NEXT
               ? USING$("Loops #",counter) 
              'Loops 1
              END FUNCTION
              Code:
              'Limit is a variable that changes in the function
              
              FUNCTION PBMAIN () AS LONG
               LOCAL x       AS LONG
               LOCAL counter AS LONG
               LOCAL limit   AS LONG
              
               limit = 1
               FOR x = 1 TO Limit  '1
                limit = 3          'limit now 3
                INCR counter       'Increment loop counter
               NEXT
               ? USING$("Loops #",counter)
               'Loops = 1
              END FUNCTION


              https://www.tesla.com/roadster

              Comment

              Working...
              X