Reputation: 41
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 :
Upvotes: 0
Views: 523
Reputation: 21033
The condition for the while
loop to continue is auxboolean = true
.
The check for auxboolean = true
is performed only when the code returns to the while
statement. What happens to auxboolean
inside the loop has no effect before that check.
The condition for the for
loop to continue is that the loop control variable i
is 0..8.
The check for i
happens equally once per loop, but has no effect on the outer while
loop. You can use break
to exit the for loop.
edit:
The peculiarity with the for
loop starting from 9 and counting down is explained in this post
I'm not sure whether this is still the case in recent releases of Delphi. I think it is, but the debugger does not show decreasing counter values.
Upvotes: 3
Reputation: 1319
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.
Upvotes: 1