PDA

View Full Version : evaluation once or every loop



primo
15-09-2016, 14:49
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 ?

ReneMiner
15-09-2016, 20:48
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

primo
16-09-2016, 08:11
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

ErosOlmi
16-09-2016, 11:56
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/products/thinBasic/help/html/index.html?fornext.htm

ReneMiner
16-09-2016, 23:00
...
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>]

?

primo
17-09-2016, 11:36
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

ErosOlmi
17-09-2016, 18:40
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.

primo
10-02-2017, 16:22
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

ErosOlmi
13-02-2017, 21:31
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} (http://www.thinbasic.com/public/products/thinBasic/help/html/index.html?fornext.htm) the last LogicalExpression is evaluated at every loop.

harsh
18-02-2017, 18:40
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.