2

I have noticed a very odd behaviour with an IF-statement in Progress 4gl.

I define an integer with the format "999" which tells it to have 3 digits and then I assign a value lower than 100 (eg. 12) then when I display it it shows as "012" as it should.

But when I add an IF-statement inside the DISPLAY statement that really shouldn't do anything, the variable is displayed as "12".

This is a test code to clearify the differences. The LABEL doesn't affect the output of the variable.

DEF VAR tmp AS INTEGER FORMAT "999".
ASSIGN tmp = 12.

DISPLAY 
    tmp LABEL "disp1".

DISPLAY
    IF TRUE THEN tmp ELSE tmp LABEL "disp2".

The same behaviour could also be acheived by changing the format to ">99".

My question is: Why does an IF-statement change the way a variable is displayed?

Best regards //MrBucket

Tom Bascom
  • 13,405
  • 2
  • 27
  • 33
MrBucket
  • 73
  • 7

1 Answers1

3

Your second example is similar to:

define variable tmp1 as integer no-undo format "999".
define variable tmp2 as integer no-undo format "99999".

display
  if true then tmp1 else tmp2
.

The compiler sees the IF function returns an integer and applies the default formatting for an integer. The compiler does not try to second guess you and notice that (in your example) both results are the same variable.

(In this case IF is a function embedded within the DISPLAY -- not a stand-alone statement of it's own.)

To get the result you are looking for:

display
  ( if true then tmp1 else tmp2 ) format "999"
.

Whenever I embed an IF function I make a point of wrapping it in parenthesis -- it helps make it clear that it is embedded and clearly shows where things like a FORMAT phrase apply.

Tom Bascom
  • 13,405
  • 2
  • 27
  • 33
  • 1
    Thanks for the answer Tom. I am a bit new to the language and it just seemed a bit odd to me. I'll remember to reformat the variable in case I come across this problem again. – MrBucket Oct 04 '12 at 11:24