BBC BASIC for Windows
« ENDWHILE »

Welcome Guest. Please Login or Register.
Apr 5th, 2018, 10:11pm



ATTENTION MEMBERS: Conforums will be closing it doors and discontinuing its service on April 15, 2018.
Ad-Free has been deactivated. Outstanding Ad-Free credits will be reimbursed to respective payment methods.

If you require a dump of the post on your message board, please come to the support board and request it.


Thank you Conforums members.

BBC BASIC for Windows Resources
Online BBC BASIC for Windows documentation
BBC BASIC for Windows Beginners' Tutorial
BBC BASIC Home Page
BBC BASIC on Rosetta Code
BBC BASIC discussion group
BBC BASIC for Windows Programmers' Reference

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: ENDWHILE  (Read 688 times)
Michael Hutton
Developer

member is offline

Avatar




PM

Gender: Male
Posts: 248
xx ENDWHILE
« Thread started on: May 11th, 2009, 4:24pm »

Why does ENDWHILE seem relatively slow?

from a profiler report:

Code:
     12134:       WHILE (this%<>0) AND (word$ > FNgetitem(index{}, this%))
     11088:       prev% = this%
     14153:       this% = index.link%
     77472:       ENDWHILE
 


surely it just loops back to the WHILE? or have I missed something?

Michael
User IP Logged

JonR
New Member
Image


member is offline

Avatar




PM


Posts: 24
xx Re: ENDWHILE
« Reply #1 on: May 11th, 2009, 8:04pm »

Ask yourself not why the ENDWHILE line is slow but why the WHILE line is quick. On reaching ENDWHILE the interpreter evaluates the WHILE condition and only continues the WHILE loop if the condition evaluates to TRUE.
User IP Logged

admin
Administrator
ImageImageImageImageImage


member is offline

Avatar




PM


Posts: 1145
xx Re: ENDWHILE
« Reply #2 on: May 11th, 2009, 10:39pm »

Quote:
surely it just loops back to the WHILE?

It doesn't loop back to the WHILE - that would be inefficient. It loops back to the conditional expression in the WHILE statement (skipping the WHILE keyword entirely) and hence the evaluation of that conditional expression is counted by the profiler as part of the ENDWHILE statement.

Another way to look at it is to realise that if the initial condition is TRUE (i.e. the body of the loop is executed at least once) WHILE...ENDWHILE is executed by the interpreter exactly the same as REPEAT...UNTIL is: the conditional test is performed at the end of the loop, not at the beginning.

Effectively the code you listed is executed by the interpreter as if it was the following:

Code:
IF (this%<>0) AND (word$ > FNgetitem(index{}, this%)) THEN
  REPEAT
    prev% = this%
    this% = index.link%
  UNTIL NOT ((this%<>0) AND (word$ > FNgetitem(index{}, this%)))
ENDIF 

Richard.
User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

| |

This forum powered for FREE by Conforums ©
Terms of Service | Privacy Policy | Conforums Support | Parental Controls