Skip to content

Instantly share code, notes, and snippets.

@omonien
Created May 4, 2024 14:01
Show Gist options
  • Save omonien/b8845c9f2b88dc71c51216b64461bb1e to your computer and use it in GitHub Desktop.
Save omonien/b8845c9f2b88dc71c51216b64461bb1e to your computer and use it in GitHub Desktop.
Demo that shows that a Delphi for loop may run in reverse order
program ForLoopExecutionOrder;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils;
begin
try
var j := 0;
var i : Integer;
{$OPTIMIZATION OFF} // We are not even in optimization mode!
for i := 0 to 10 do // Set a break point here, open CPU view and notice:
begin // mov eax,$0000000b -> start with 11
inc(j); // dec eax -> decrement by one
end; // ==> Loop runs in reverse order as compiler detected that the actual order is unimportant
Assert(j = 11); //Simple sanity check
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
readln;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment