What is Control Statements?


Control statements are the statements that change the flow of execution of statements. For example, If, If-else, Switch-Case, while-do statements. In programming languages, Boolean expressions are used to

  • Alter the flow of control− Boolean expressions are used as conditional expressions in a statement that alter the flow of control. The value of such Boolean expression is implicit in a position reached in a program. For example, if (E) S, the expression E should be true if statement S is reached.

  • Compute logical values− A Boolean expression can define true or false values. Such Boolean expressions can be computed in parallel to arithmetic expressions using three address instruction with logical operators.

The designed use of the Boolean expression is decided by its syntactic context. For example, an expression following the keyword ‘if’ is used to alter the flow of control while an expression on the right side of an assignment can indicate a logical value. Such syntactic contexts can be defined in several ways. It can use several nonterminals, use inherited attributes, or set a flag during parsing. It can generate a syntax tree and invoke multiple procedures for the two different uses of Boolean expressions.

Example1− Generate three Address Code for

while (a < b) do

If (c < d) then

   x = y + z

else

   x = y – z.

Solution

(1) If a < b goto(3) 

(2) goto(11) 

(3) If c < d goto(5) 

(4) goto(8) 

(5) t1 = y + z 

(6) x = t1 

(7) goto(1) 

(8) t2 = y − z 

(9) x = t2 

(10) goto(1)

Example2− Generate three Address Code for

while (A < C and B < D) do
If A=1 then C = C + 1
else while A≤D do A = A + 2.

Solution

(1) If A < C goto(3)

(2) goto(15)

(3) If B < D goto(5)

(4) goto(15)

(5) If A = 1 goto(7)

(6) goto (10)

(7) T1 = C + 1

(8) C = T1

(9) goto (1)

(10) If A ≤ D goto (12)

(11) goto (1)

(12) T2 = A + 2

(13) A = T2

(14) goto (10)

Example3− Generate three Address Code for

Switch (a)
{
   Case 1−
      b = c + d
      break;
   Case 2−
      e = f + g
      break;
}

Solution

(1) If a = 1 goto(3)

(2) If a = 2 goto (6)

(3) t1 = c + d

(4) b = t1

(5) goto(9)

(6) t2 = f + g

(7) e = t2

(8) goto(9)

Example4− Generate three address code for

while (i<5)
{
   i=i+1;
}

Solution

(1)if i<5 goto(3)

(2) goto (5)

(3) i = i + 1

(4) goto (1)

Example5− Convert the following control statements into Three Address Code.

If (A < B 𝐎𝐑 C > D)
X = X + Y + Z

Solution

(1) If A < B goto(4)

(2) If C > D goto(4)

(3) goto(7)

(4) T1− = X + Y

(5) T2 = T1 + Z

(6) X− = T2

Updated on: 05-Nov-2021

17K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements