delphidelphi-2006

Why loop While with for inside doen't work?


I'm trying to break out of the while loop by changing the auxboolean variable inside the for loop but it doesn't work. The for loop doesn't exit when auxboolean is set to false. Why?

Also, the for loop seems to start with i having value 9 and counts backwards. Why?

auxboolean:= true;
    while auxboolean do
    begin
      for i := 0 to 8  do
      begin
       auxboolean:=false;
      end;
    end;

When debugging you can see that the first value for i is 9 and it then counts down :

Debug

enter image description here

enter image description here


Solution

  • I think you want to do something like that:

    begin
      auxboolean:=true;
    
      for i := 0 to 8 do
        begin
          Writeln(i);
    
          auxboolean:=false;
    
          if not auxboolean then break;
        end;
    
      Writeln('End of the program...');
      Readln;
    end.
    

    You don't have to use the while loop, instead of it see the break statement.