I have somehow hard time in understanding the following program in LOOP (you can also read the article in its original German), which is supposed to simulate IF x=0 THEN A END.
y:=1;
LOOP x DO y:=0 END;
LOOP y DO A END;
How exactly does this simulate an IF statement? Why can’t I do this:
x:=0;
LOOP x DO A END;
I have no idea what this language is but if one assumes that
LOOP cntr DO stmt END;executesstmtas long ascntris greater than zero (and it’s decremented after each iteration), then this works as follows:1is assigned toy. Ifxis greater than zero, thenyis assigned0. Ifxis zero the value ofyremains1. In the next loop,Ais executed once ifyis1which is the case only if the previousLOOPstatement didn’t execute, that is ifxwas equal to0. The main advantage of this construct is thatAis executed only once and not as much times as is the value ofxif it is greater than zero whichLOOP x DO A END;would do.Here is the logic once again, step by step:
yis set to1yis set to zeroxtimes. This is equivalent toIF x <> 0 THEN y:=0 ENDAis executedytimes (once ifxis zero and zero times ifxis non-zero)Those three steps translate to: if
xis zero, executeAonce, otherwise executeAzero times (i.e. don’t execute).