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

if expression  then statement $1$

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

1 Answer

+6 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$:
by Active (2k points)
edited by
0
That means ,  in template "condition statement 1" & " condition statement 2" should be swapped to make it correct.
+3
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,647 questions
56,492 answers
195,439 comments
100,697 users