World's most popular travel blog for travel bloggers.

Can LOOP-Programm stop when its value goes below 0?

, , No Comments
Problem Detail: 

I am wondering, if in the LOOP programing language, whether instances of the LOOP x DO P END are defined to stop in the case $x < 0$.

The definition only says "Execute $P$, $x$ times" for the above program.

IT is perfectly clear for $x = 0$. However, I believe in order to keep the language simple the definition doesn't extend to the case $x < 0$.

If it were possible, then it would for instance basically shorten a $x_1\ div\ x_2$ operator to just one line:

x_0 = 0; LOOP x_1 DO x_1 = x_1 - x_2; x_0 = x_0 + 1; END 

If it is not possible, is there any other reason except for reasons of convention?

Asked By : user8
Answered By : Yuval Filmus

Variables in LOOP programs are non-negative, so the problem never arises; the instruction $x_1 = x_1 - c$ assigns $x_1$ the value of $\max(x_1-c,0)$.

Note that the instruction $x_1 = x_1 - x_2$ isn't allowed: the second operand has to be a constant. However, since $x_1 = x_1 - x_2$ can be implemented in LOOP, the programming language "LOOP with instructions of the form $x_i = x_i - x_j$" has the same power as LOOP.

Best Answer from StackOverflow

Question Source :

3200 people like this

 Download Related Notes/Documents


Post a Comment

Let us know your responses and feedback