Possibly better loop construct, also labels+goto important and on the fly compiler idea.

Bernhard Schornak schornak at web.de
Mon Oct 28 06:49:26 EDT 2013


Due to unknown "improvements", SeaMonkey's built-in news editor meanwhile ignores
saved changes and sends long deleted text parts rather than thwe last seen text -
"What you See Is _Not_ What You Get"...

This is the real reply to Skybuck's posting. Please ignore the mixture of deleted
text parts, older and newer text posted at 10:58 h.  Unfortunately, the option to
remove messages from the server became the victim of another "improvement", so it
is impossible for me to delete the other post.

Bernhard Schornak



Skybuck Flying wrote:


> Because it's logical.


What is logical?


> If the exit condition was placed on the top, the loop would exit immediatly.


This might be the programmer's intention?


> Instead the desired behaviour might be to execute the code inside the loop first and then exit.


With a 50 percent chance it might not. Are we fortune tellers who guess which one
might be true?


> Thus seperating logic into enter and exit conditions makes sense.


Let us assume a loop with variable iterations where the iteration count is passed
as function parameter. The code inside the loop body uses this iteration count as
index into an array and alters data in other arrays depending on computations and
set flags.

If we probed the loop's repeat condition at the end of the loop body per default,
we had to verify the passed iteration count before we enter the loop - otherwise,
the program crashed with erroneously passed negative numbers. We also had to sort
out zero. Otherwise, we started a count down through the full 32 or 64 bit range.
The last problem pops up, if the loop's repeat condition isn't met. In this case,
we had to restore all altered data before we could continue with the instructions
following the loop body.

Programming on machine language level grants us the freedom to create the fastest
possible code. It is no good idea to slow down our code just to obey questionable
"Universal Laws".


BTW: I placed label 0 at the wrong place. The proper loop should look like this:

func:...
      ...
    0:dec     rcx
      jbe     1f
      ...
      some
      code
      to
      perform
      ...
      jmp     0b

      p2align 5,,31
    1:continue
      with
      something
      else
      ...


Greetings from Augsburg

Bernhard Schornak




More information about the Python-list mailing list