Results 1 to 10 of 10

Thread: evaluation once or every loop

Share/Bookmark
  1. #1

    evaluation once or every loop

    suppose
    String st
    st = "12345"
    Long i, t
    For i=1 To Len(st)^2
    t+i
    Next
    MsgBox 0, t
    
    1- is the Len(st)^2 evaluated every time the loop executed, or once and replaced by a number internally
    2- is it the same situation using a compiler ?

  2. #2
    thinBasic MVPs ReneMiner's Avatar
    Join Date
    Oct 2012
    Location
    Germany
    Age
    47
    Posts
    1,214
    Blog Entries
    1
    Rep Power
    132
    well, let's test it. Simply change the length of string st.
    If we add some char every loop it should run infinite if Len(st)^2 gets calculated every time.

    Uses "console"
    
    String st = "12345"
    Long i
    
    For i = 1 To Len(st)^2 
      Print "iterated i up to "
      Print i In 15 
      Print " while st = """
      Print st In 15 
      PrintL $DQ
      
      st &= Hex$(i,2)  
      
      
     
    Next
    WaitKey
    
    Now what would you say?

    By the way, For-Next has the ability to check for a special condition in thinBasic using keyword When so that gets tested every loop.
    Check this out:
    Uses "console"
    
    String st = "12345"
    Long i
    
    For i = 1 To Len(st)^2 When LEFT$(st, 3) = "123"
      Print "iteration " 
      PrintL i In 15
      
      If i = 17 Then 
        st = "0" & st
      EndIf
      
    Next
    WaitKey
    
    Windows 10 on
    Desktop: Intel Core i5-3330 @3.0 GHz, 8 GB Ram, nVidia GeForce GT630 2 GB
    Laptop: Intel Core i3-4005U @1.7GHz, 8 GB Ram, nVidia GeForce 940M 2 GB


    The limit is Imagination

  3. #3
    Thanks for the reply and the interesting codes
    depending on your very good idea about increasing the string length gradually, i think it is evaluated previously as a fixed number:
    For i = 1 To Len(st)^2 = For i=1 to 25
    Uses "console"
    
    String st = "123"
    Long i
     
    For i = 1 To Len(st)
      st = st & "a"
      PrintL i
    Next
    WaitKey
    
    it stops after 3 Loops
    i read somewhere that the interpreters evaluate the code lines continuously. i will test GWBasic in DosBox , will report back if different than thinbasic result

  4. #4
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    50
    Posts
    8,114
    Blog Entries
    2
    Rep Power
    10
    Ciao,

    in case of FOR/NEXT, the TO upper limit is actually evaluated just once in order to speed up loop operations.
    FOR ... TO ... line is evaluated only at the beginning. When equivalent NEXT is encountered, execution pointer is set to the first byte of the source code just following the FOR ... TO ... statement.

    Instead if FOR ... TO ... has a WHILE/UNTIL/WHERE clause, WHILE/UNTIL/WHEN clause is evaluated at every loop, but only the WHILE/UNTIL/WHEN clause and not the full FOR statement.

    If you do not know before the number of loops needed to execute some code because it is variable, I suggest to use other loops statement different from FOR/NEXT
    WHILE/WEND, DO/LOOP always test the condition at every loop. That's why they are in general slower than FOR/NEXT

    Ciao
    Eros

    Help: http://www.thinbasic.com/public/prod...ml?fornext.htm
    Last edited by ErosOlmi; 17-09-2016 at 18:37.
    www.thinbasic.com | www.thinbasic.com/community/ | psch.thinbasic.com
    Win10Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

  5. #5
    thinBasic MVPs ReneMiner's Avatar
    Join Date
    Oct 2012
    Location
    Germany
    Age
    47
    Posts
    1,214
    Blog Entries
    1
    Rep Power
    132
    Quote Originally Posted by ErosOlmi View Post
    ...
    Instead if FOR ... TO ... has a WHILE/UNTIL/WHERE clause, WHILE/UNTIL/WHERE clause is evaluated at every loop, but only the WHILE/UNTIL/WHERE clause and not the full FOR statement.
    ...
    WHERE? Did i miss something? Did you mean WEND or WHEN?
    I'm only familiar with

    While <condition>
    
    Wend [Until <condition>]
    
    Is it the same lower speed with

    Do [While <condition]
    
    Loop [{While|Until} <condition>]
    
    as with For-Next?

    And could we use keyword "When" also as
    While <condition>
    
    Wend [{Until|When} <condition>]
    
    ?
    Windows 10 on
    Desktop: Intel Core i5-3330 @3.0 GHz, 8 GB Ram, nVidia GeForce GT630 2 GB
    Laptop: Intel Core i3-4005U @1.7GHz, 8 GB Ram, nVidia GeForce 940M 2 GB


    The limit is Imagination

  6. #6
    WHERE
    if it is not a typo mistake, then it is : let me imagine: 1- it is a new function in experimental TB version. 2-what usage "WHERE" can be used for ? i think it can be a search function and it may return a place and a position of something like a text, but not sure in what context !! just imagination.
    and thanks Eros for the full description of iterations functions.

    edit: no it is not a search function, since in a typo mistake it is added to the iteration functions
    Last edited by primo; 17-09-2016 at 11:41.

  7. #7
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    50
    Posts
    8,114
    Blog Entries
    2
    Rep Power
    10
    Yes, sorry it is a typo. It is WHEN and not WHERE.
    WHEN followed by a logical condition is used to iterate the FOR statement WHEN condition is false.
    I have to document it.
    www.thinbasic.com | www.thinbasic.com/community/ | psch.thinbasic.com
    Win10Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

  8. #8
    i have done some experiments with other languages, this is the results:
    in quickbasic v4.5 (using DosBox)
    s$ = "123"
         FOR i=1 TO LEN(s$)
         s$=s$+"a"
         PRINT i
         NEXT i
    
    result in 3 Loops, i am sure the same with GWBasic
    ************************
    in Power Basic 3.5:
      s$ = "123"
        DIM i AS LONG
        FOR i=1 TO LEN(s$)
         s$=s$+"a"
         PRINT i
         NEXT i
    
    the same loop 3 times
    **************************

    in purebasic
    st.s = "123"
    For i = 1 To Len(st)
      st = st + "a"
      Debug i
      If i=1000:Break:EndIf
    Next
    
    it reevaluate len(st) contineously, so the user should beware if the demo is a time consuming code such as a fractal and evaluate it before the Loop
    i will test other compilers later

    Edit: testing freebasic, the same as thinbasic, powerbasic, quick basic. so it appears purebasic is the only one to evaluate For i = 1 To Len(st) for every loop
    Last edited by primo; 12-02-2017 at 10:01.

  9. #9
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    50
    Posts
    8,114
    Blog Entries
    2
    Rep Power
    10
    It is not complex to create a dynamic FOR instead of a deterministic FOR.
    It would result into a slower FOR because you have to evaluate the TO clause at every loop.
    Especially for thinBasic, that is an interpreted language, it would result into a quite slower FOR

    In thinBasic when FOR is used with one of the optional {WHILE | UNTIL | WHEN} the last LogicalExpression is evaluated at every loop.
    Last edited by ErosOlmi; 13-02-2017 at 21:35.
    www.thinbasic.com | www.thinbasic.com/community/ | psch.thinbasic.com
    Win10Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

  10. #10
    Quote Originally Posted by ErosOlmi View Post
    It is not complex to create a dynamic FOR instead of a deterministic FOR.
    As you pointed out, there are more conventional ways to implement a dynamic end condition or a variable STEP. The FOR loop should be used where the number of iterations is known and the increment is fixed. It is not the basis for an event handling system.

    If you need to change the limits or the STEP during execution of the loop, use DO UNTIL for post-check or WHILE for pre-check.

Similar Threads

  1. Problems with Arrays In For/Next Loop
    By peralta_mike in forum Core module
    Replies: 4
    Last Post: 17-05-2011, 15:53
  2. Self-evaluation reference chart
    By ErosOlmi in forum General
    Replies: 0
    Last Post: 07-07-2008, 18:29
  3. Repeat or loop
    By Henry in forum thinBasic General
    Replies: 1
    Last Post: 30-11-2007, 14:24
  4. Usage of DO/LOOP, DO WHILE/LOOP, DO / LOOP UNTIL
    By Petr Schreiber in forum Samples for help file
    Replies: 2
    Last Post: 29-05-2007, 16:06

Posting Permissions

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