in Compiler Design edited by
1,849 views
5 votes
5 votes

Is the following code template for the if-then-else statement correct? if not, correct it.

  • $\text{if} \text{ expression then statement } 1$
    $\text{else statement }2$

Template:

Code for expression

  • (*result in $E, E > O$ indicates true *)
  • Branch on $E > O$ to $L1$
  • Code for statement $1$
  • $L1$: Code for statement $2$
in Compiler Design edited by
1.8k views

2 Answers

18 votes
18 votes
Best answer

The given template is wrong. The following should be correct:

Code for Expression

  • Branch on $E>0$ to $L$
  • Code for segment $2$
  • Branch to $L1$
  • $L$: Code for statement $1$
  • $L1$:
edited by

4 Comments

That means ,  in template "condition statement 1" & " condition statement 2" should be swapped to make it correct.
2
2
After swapping statement 1 and statement 2, an unconditional branch instruction should be also included after statement 2. Otherwise both the statements will get executed, which should not happen.
7
7
Why would this →  L: Code for statement 1 execute again? after executing above code. L would execute then and then only when L is called particularly by IF right?

What is the significance of this line → Branch to L1 and L1 :

Please explain.
0
0

I think it is for exit.

0
0
0 votes
0 votes

What is expected from the code:

“if expression is true execute statement 1, else execute statement 2.”

What given code is doing:

“if expression (E > 0) is true execute statement 2, else execute statement 1.”

Modified Template:

Code for expression

  • (*result in E, E>O indicates true *)
  • Branch on E>O to L1
  • Code for statement 2
  • L1: Code for statement 1

Related questions