SRL Archive - Bugs

while vs. repeat


marpis

2011-12-22 15:32:48

For some strange reason,
while BooleanFunction() do
Stuff;
kept doing Stuff() although BooleanFunction() did not return true.

I changed it to

repeat
Stuff;
until(not BooleanFunction);
and it solved the problem.


????

Main

2011-12-22 16:48:02

Is it doing the same for any condition?

DaWu

2011-12-22 18:17:59

I've had while do loops act weird too, thats why I dont currently use them.

Daniel

2011-12-22 21:11:57

Neither loop should differ, only one being a pre-test (the while) while the other is a post-test (repeat) loop.

Are you sure there is nothing in Stuff() that affects the result of BooleanFunction()?

Send your script over.

marpis

2011-12-23 01:38:29

From: Daniel
Neither loop should differ, only one being a pre-test (the while) while the other is a post-test (repeat) loop.

Are you sure there is nothing in Stuff() that affects the result of BooleanFunction()?

Send your script over.


Yes I'm positive. The code worked perfectly with repeat loop, and got stuck in an infinite loop with while loop.

Daniel

2011-12-23 02:19:10

From: marpis
Yes I'm positive. The code worked perfectly with repeat loop, and got stuck in an infinite loop with while loop.


How many iterations of your loop are you expecting?

marpis

2011-12-23 02:27:22

From: Daniel
How many iterations of your loop are you expecting?


The BooleanFunction was a custom InFight(), and it just didn't break from the loop when I used while loop, but with repeat it worked.

Swatarianess

2011-12-23 02:51:35

Depending on the use it'd do different things?

for example;

while i=5 do
writeln('We have:'+inttostring(i)+ 'items')
i guess somewhere in the while loop you'd have to put an If to exit?



if(i=5) then
repeat
Action1;
until(not i=5)
This is what you'd do with repeat in this situation? with if you can do more in this situation.

not sure about it but this is what i see?

KingKong

2011-12-23 09:40:02

From: marpis
Yes I'm positive. The code worked perfectly with repeat loop, and got stuck in an infinite loop with while loop.


By perfect you mean it went through one iteration of the repeat loop? Because a repeat loop breaks out if the test is true whereas the while loop breaks out if the test is false.

eXoTiK

2011-12-23 11:57:01

Did you use the correct structure for the loop? It should be like..


while [boolean] do
begin
stuff;
end;
until [boolean]

m34tcode

2011-12-23 12:01:16

A repeat loop will always execute at least once, since it is a post test like Daniel said. Could that be the problem?

EDIT: no that Cant be. Does simba have its own loop programmed? Or does it have f Pasca
l do it

Shuttleu

2011-12-23 12:02:48

From: eXoTiK
Did you use the correct structure for the loop? It should be like..


while [boolean] do
begin
stuff;
end;
until [boolean]


thats incorrect
structures are like so

while [boolean] do
begin
stuff;
end;

repeat
stuff;
until [boolean]

From: m34tcode
A repeat loop will always execute at least once, since it is a post test like Daniel said. Could that be the problem?

no it isnt

from what i understand the while loop is still executing even though the bool is false

~shut

Main

2011-12-23 12:04:13

I think they might be blaming simba for the cause, I had problem like this before ( not in loops) when I was trying to make a function where the variable replaces it self or something like that, where my logic should be correct but its not letting me do what I want.

I might be wrong but try reinstall simba? :(

Shuttleu

2011-12-23 12:05:58

Marpis
try this

while BooleanFunction do
begin
writeln(BooleanFunction);
Stuff;
end;

Wanted

2011-12-23 12:21:00

function RandomBoolean: Boolean;
begin
Result := (Random(10) = 1);
end;

begin
while (not (RandomBoolean)) do
WriteLn('while True');
repeat
WriteLn('repeat True');
until (RandomBoolean);
end.

Worked fine?

Daniel

2011-12-23 21:01:44

From: Wanted
function RandomBoolean: Boolean;
begin
Result := (Random(10) = 1);
end;

begin
while (not (RandomBoolean)) do
WriteLn('while True');
repeat
WriteLn('repeat True');
until (RandomBoolean);
end.

Worked fine?


Yeah, I did the same thing and it worked for me.

I'm starting to blame PascalScript for this issue, and IIRC, it was (or something similar, involving a while loop) a bug in previous versions of PascalScript?