The Gateway to Computer Science Excellence
+1 vote
Is the following code template for the if-then-else statement correct? if not, correct it.

if expression  then statement $1$

                         else statement $2$


            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 by Boss (30.8k points)
edited by | 511 views

1 Answer

+7 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$

by Active (2k points)
edited by
That means ,  in template "condition statement 1" & " condition statement 2" should be swapped to make it correct.
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.

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,737 questions
57,377 answers
105,312 users