2
$\begingroup$

We have the following operation $\text{IF} \ x_i =0 \ \text{THEN} \ P \ \text{END}$. I want to simulate this using a $\text{LOOP}$ program. Here is what I have:

$$\text{IF} \ x_i = 0 \ \text{THEN} \ P \ \text{END} \ ≙ \begin{cases} x_j := 1; \\ \text{LOOP} \ x_i \ \text{DO} \ x_j := 0 \ \text{END}; \\ \text{LOOP} \ x_j \ \text{DO} \ P \ \text{END} \end{cases} $$

The $\text{IF}$ program executes $P$ only when $x_i =0$, otherwise terminating. The $\text{LOOP}$ program takes $x_j$ and assigns it the value $1$. Then loops $x_i$ many times executing the program $x_j := 0$. Since no value has been assigned to $x_i$, this line is looped over $0$ many times. In the final line we loop once and execute the program $P$. Therefore, both the programs are equivalent.

So if we have $x_i = 3$, then we loop $3$ times, and each time we assign $x_j$ the value of $0$. After the third loop, the program terminates and never executes the program $P$. This is the same as the $\text{IF}$ program, which doesn't execute $P$ since $x_i \neq 0$. Is my understanding correct?

$\endgroup$
4
  • $\begingroup$ What is your question? $\endgroup$ Jan 11, 2021 at 14:47
  • $\begingroup$ @YuvalFilmus If my understanding is correct or not. $\endgroup$
    – Ski Mask
    Jan 11, 2021 at 15:20
  • 2
    $\begingroup$ Can you prove that your LOOP program simulates IF? If so, then it works correctly. If not, where are you stuck in your proof? $\endgroup$ Jan 11, 2021 at 15:21
  • 1
    $\begingroup$ We discourage "please check whether my answer is correct" questions, as only "yes/no" answers are possible, which won't help you or future visitors. See here and here. Can you edit your post to ask about a specific conceptual issue you're uncertain about? As a rule of thumb, a good conceptual question should be useful even to someone who isn't looking at the problem you happen to be working on. If you just need someone to check your work, you might seek out a friend, classmate, or teacher. $\endgroup$
    – D.W.
    Jan 11, 2021 at 18:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.