# GATE1995-1.4

1.2k views

In the following Pascal program segment, what is the value of X after the execution of the program segment?

X := -10; Y := 20;
If X > Y then if X < 0 then X := abs(X) else X := 2*X;
1. $10$
2. $-20$
3. $-10$
4. None

edited

Answer of $X$ remains unchanged. As the if condition becomes false.

X := -10

Answer is C . This is classic example of $if-else$ issue. Always $else$ matches for nesting to the closest $if$  in $C$ Programming & Pascal .
https://en.wikipedia.org/wiki/Dangling_else

if (x>y)
{
if (x<0)
x=abs(x)
else
x=2*x
}

edited
0
else matches to closest if in all languages?
3
For sure in C & Pascal ! We don't get any other languages in GATE !
2
But here x < y i.e.  -10 < 20
0
Yes you seem correct. Updated.
2
When having confusion, always try to match else with last unmatched if condition.

This is usually the solution the compiler follows for dangling else problem during compilation of a program.

Reference: Ullman for compilers
0

always try to match else with last unmatched if condition.

means?

0
Means

if(a>b) then if(b>c) then t=x else v=y will be

if(a>b) then

if(b>c) then

t=x

else

v=y
X is remains unchanged. As the if condition is becomes false.
X = -10

Hope this helps:)

## Related questions

1
1.1k views
Consider the following Pascal function where $A$ and $B$ are non-zero positive integers. What is the value of $GET(3, 2)$? function GET(A,B:integer): integer; begin if B=0 then GET:= 1 else if A < B then GET:= 0 else GET:= GET(A-1, B) + GET(A-1, B-1) end; The Pascal procedure given for computing the ... 1 to N - 1 do for J:=1 to N do begin TMP:= A[I, J]; A[I, J]:= A[J, I]; A[J, I]:= TMP end end;
Assume that $X$ and $Y$ are non-zero positive integers. What does the following Pascal program segment do? while X <> Y do if X > Y then X := X - Y else Y := Y - X; write(X); Computes the LCM of two numbers Divides the larger number by the smaller number Computes the GCD of two numbers None of the above
Consider the following high level programming segment. Give the contents of the memory locations for variables $W, X, Y$ and $Z$ after the execution of the program segment. The values of the variables $A$ and $B$ are $5CH$ and $92H$, respectively. Also indicate error conditions if any. var A, B ... integer, (each integer is represented by two bytes) begin X :=A+B Y :=abs(A-B); W :=A-B Z :=A*B end;