Announcement

Collapse
No announcement yet.

A simple For/Next question

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

  • A simple For/Next question

    It seems if the counter variable of the For/Next loop is carried to a subroutine, the counter reverses the sequence. Example:



    global stpp as integer

    function pbmain() as long

    '' stpp goes 1 to 5
    for stpp=1 to 5
    ? str$(stpp)
    next stpp

    '' stpp goes 5 to 1
    for stpp =1 to 5
    call test1
    next stpp

    end function

    sub test1
    ? str$(stpp)
    end sub



    HYC

  • #2
    Very interesting!

    If you pass the value as a parameter instead of a global, it behaves differently:

    Code:
    #compile exe
    #debug display
    #register none
    
    function pbmain() as long
    local stpp as long ' integer - same result
    
    for stpp = 1 to 5
         call test1(stpp)
    next stpp
    
    end function
    
    sub test1(l as long)
    ? str$(l), %mb_taskmodal
    end sub
    Another good reason to avoid Globals?

    Comment


    • #3
      Yet if you make tyhe FOR index a descending one, the order is correct:
      Code:
      #compile exe
      #debug display
      #register none
      global stpp as long
      
      function pbmain() as long
      
      for stpp = 5 to 1 step -1
           call test1
      next stpp
      
      end function
      
      sub test1
      ? str$(stpp), %mb_taskmodal
      end sub

      Comment


      • #4
        It is know that if the counter variable isn't referenced in the code inside the FOR NEXT loop, the loop is optimized to count down instead of up for improved execution speed.
        Forum: http://www.jose.it-berater.org/smfforum/index.php

        Comment


        • #5
          Originally posted by José Roca View Post
          It is know that if the counter variable isn't referenced in the code inside the FOR NEXT loop, the loop is optimized to count down instead of up for improved execution speed.
          Actually it's in the online HELP

          In certain situations, previous versions of PowerBASIC optimized FOR/NEXT loops to count down instead of up for improved execution speed. This optimization could cause the counter variable to contain a value which was not expected when execution of the loop was complete. This optimization has been improved so that the counter variable value is always correct upon loop completion, even if EXIT FOR was used to force an early termination.
          If you read this quickly, you might get the impression that the current version should not do this. Moral - read the online help system slowly, if necessary with lip movements.

          Comment


          • #6
            > Another good reason to avoid Globals?

            Actually it's another good reason to avoid using program variables which contain actual application data as loop counters.
            Michael Mattias
            Tal Systems Inc. (retired)
            Racine WI USA
            [email protected]
            http://www.talsystems.com

            Comment


            • #7
              Has BASIC ever allowed the stop value to be increased during a FOR/NEXT loop. (I had a need for this but it didn't work)

              Code:
              Dim array(20) as string
              local count as long
              local temp as string
              
              register i as long
              
              count = 10
              for i = 1 to count
                temp = array(i)
                if temp = "something strange" then
                  array(11) = "new string"
                  incr count
                end if
              next i

              Comment


              • #8
                Originally posted by José Roca View Post
                It is know that if the counter variable isn't referenced in the code inside the FOR NEXT loop...
                In an ideal world (I know!) this behaviour would not be the default, because it is darn confusing. Maybe a compiler directive would help?

                In the same ideal world, the online help would clearly seperate the explanation of the default behaviour (i.e. your for/next loop might run backwards) with the explanation of the change in behaviour in the current version. For example,

                WARNING - If the counter variable is not referenced within the loop code itself, the compiler may reverse the counting direction from upwards to downwards. Even if the variable is used by SUBS or FUNCTIONS called from within the loop. We do this to make the generated code faster.

                Note also that the counter variable will always have the final value implied by the FOR..NEXT statement, whether or not the counter reversal mentioned above has occurred. This is a change from previous versions.

                Comment


                • #9
                  >Has BASIC ever allowed the stop value to be increased during a FOR/NEXT loop

                  I don't know if any interpreters or compilers ever allowed it; however, "DO/LOOP WHILE/UNTIL" and "EXIT" moot the requirement .

                  Plus I think changing the range of a FOR/NEXT while the loop is executing is silly, but that's just one man's opinion. A loop counter is a loop counter, not a control or application variable.
                  Michael Mattias
                  Tal Systems Inc. (retired)
                  Racine WI USA
                  [email protected]
                  http://www.talsystems.com

                  Comment


                  • #10
                    if really needed to code like this;
                    the work-around is to use the variable in then loop

                    Code:
                    Global stpp As Integer
                     
                    Function PBMain() As Long
                        Local dummy As Long
                     
                        '' stpp goes 1 to 5
                        For stpp=1 To 5
                            ? Str$(stpp)
                        Next stpp
                     
                        '' stpp goes 5 to 1
                        For stpp =1 To 5
                            dummy = stpp
                            Call test1
                        Next stpp
                     
                    End Function
                     
                    Sub test1
                        ? Str$(stpp)
                    End Sub
                    stanthemanstan~gmail
                    Dead Theory Walking
                    Range Trie Tree
                    HLib ~ Free Data Container Lib ~ Arrays, Lists, Stacks, Queues, Deques, Trees, Hashes

                    Comment


                    • #11
                      Originally posted by Michael Mattias View Post
                      >Has BASIC ever allowed the stop value to be increased during a FOR/NEXT loop

                      I don't know if any interpreters or compilers ever allowed it; however, "DO/LOOP WHILE/UNTIL" and "EXIT" moot the requirement .

                      Plus I think changing the range of a FOR/NEXT while the loop is executing is silly, but that's just one man's opinion. A loop counter is a loop counter, not a control or application variable.
                      I've done this many times in the past. When using Applesoft BASIC on the Apple II (and probably BASIC on Commodore and TRS-80 machines as well), there was no command to prematurely exit a FOR/NEXT loop so I would often end up with code like this:

                      Code:
                      10 FOR I = 1 TO 100
                      20 IF AReasonToQuitEarly = True THEN I = 100: GOTO 40
                      30 REM No reason to quit early so do something
                      40 NEXT I
                      There is no need to do this sort of thing with PowerBASIC and if I want to know the index of the FOR/NEXT loop when I leave early, I usually put it in a variable before doing the EXIT FOR. I don't think I've ever modified the index of a FOR/NEXT for any reason other than the above.
                      Jeff Blakeney

                      Comment


                      • #12
                        >there was no command to prematurely exit a FOR/NEXT

                        I think we all did a lot of things 'back in the day', as the kids say.

                        But with the modern compilers and operating systems and all the options, seems to me it's not only no longer necessary for variables to do "double duty," it's now good programming practice to avoid that "double-duty," too.

                        Even after working in Win/32 for 10+ years now, I still cringe a little at first when a write something which is going to require TWO WHOLE MEGABYTES OF MEMORY! YIKES!

                        (Old habits never die, they are simply reclassified "legacy style code.")
                        Michael Mattias
                        Tal Systems Inc. (retired)
                        Racine WI USA
                        [email protected]
                        http://www.talsystems.com

                        Comment


                        • #13
                          This is odd that this question comes up just when I was thinking of making an NFS that included the option of changing the stop value.

                          The idea I had was of EVACUATEing the FOR/NEXT loop with the option of REENTERing and upon return optionally changing the Stop value.

                          I have given it some considerable thought, and as Michael states, there are existing tools to handle every situation. When you throw in nesting of loops, we seem to do okay.

                          But on the other hand, some of the convolutions we have had to go through as programmers might be mitigated by such a tool and language adjustment.
                          Rod
                          I want not 'not', not Knot, not Knott, not Nott, not knot, not naught, not nought, but aught.

                          Comment

                          Working...
                          X