Kbase P22793: How to do a conditional ASSIGN using WHEN and IF THEN ELSE statements
Autor |
  Progress Software Corporation - Progress |
Acesso |
  Público |
Publicação |
  16/10/2008 |
|
Status: Verified
GOAL:
How to do a conditional ASSIGN using WHEN and IF THEN ELSE statements
GOAL:
What is the difference between WHEN and IF THEN ELSE in a ASSIGN statement
FACT(s) (Environment):
Progress 8.x
Progress 9.x
FIX:
Since Progress 8, ASSIGN statement has a new option: WHEN.
ASSIGN {
[ [ INPUT ] FRAME frame | BROWSE browse ]
{ field [ = expression ] } [ WHEN expression ]
} ... [NO-ERROR]
WHEN <expression>: moves data to the record buffer only when the expression used in the WHEN option has a value of TRUE. Expression is a field name, variable name, or expression whose value is logical which is evaluated only once at the begin of the ASSIGN statement.
WHEN option provide the possibility to use an expression evaluated at the beginning of the ASSIGN statement
IF..THEN..ELSE provide the possibility to use an expression evaluated at the assign time.
So these two options complete each other - you are allowed to do any conditional assignment in just one ASSIGN statement.
/* Example1.p */
DEF VAR a AS INTEGER INITIAL 1.
DEF VAR b AS INTEGER INITIAL 2.
DEF VAR c AS INTEGER INITIAL -1.
ASSIGN
a = b
c = 100 WHEN (a = b).
DISPLAY a b c.
/* >>> result: c = -1 */
/* Example2.p */
DEF VAR a AS INTEGER INITIAL 1.
DEF VAR b AS INTEGER INITIAL 2.
DEF VAR c AS INTEGER INITIAL -1.
a = b.
ASSIGN
c = 100 WHEN (a = b).
DISPLAY a b c.
/* >>> result: c = 100 */
/* Example3.p - Conditional assign Using IF..THEN..ELSE */
DEF VAR a AS INTEGER INITIAL 1.
DEF VAR b AS INTEGER INITIAL 2.
DEF VAR c AS INTEGER INITIAL -1.
ASSIGN
a = b
c = IF a = b THEN 100 ELSE c.
DISPLAY a b c.
/* >>> result: c = 100 */