The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+11 votes
755 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
in Algorithms by Veteran (52.1k points)
edited by | 755 views

1 Answer

+23 votes
Best answer

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
}
by Boss (41.1k points)
edited by
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.
+1
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

@Ayush Upadhyaya

I havenot got your point

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

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
49,807 questions
54,713 answers
189,261 comments
79,700 users