if vs. $ (conditional)

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #000000; background-color: #ffffff} span.s1 {font-variant-ligatures: no-common-ligatures}

http://code.kx.com/wiki/Reference/DollarSign#cond

/ what is the difference between using $ (conditional) and if?

/ does if evaluate the expression but not “return” it?

q)$[0b;true;false]

`false

q)if[0b;true;false] / why isn’t `false output?

q)

q)($[0b;true;false])~(if[0b;true;false])

0b

q)($[0b;true;false])~($[0b;true;false])

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #000000; background-color: #ffffff} span.s1 {font-variant-ligatures: no-common-ligatures}

1b

Hi,

Yes you are correct, an ‘if’ statement will execute all following statements if the check is true BUT will not return anything. 

The $ conditional is an if-else statement which does have a return value.

q)if[1b;a:10;b:20;c:30]

q)a

10

q)b

20

q)c

30


q)if[0b;d:100;f:200]

q)d
'd //undefined as it wasn't executed
q)f
'f //undefined as it wasn't executed

************************

$[true;do this;else do this]

or

$[if cond1 true;do this;else check cond 2;if true do this;else do this]


q)$[1b;“yes”;“no”]

“yes”

q)

q)$[0b;“yes”;“no”]

“no”


q)$[0>1;“cond 1 true”;0<1;“cond 2 true”;“cond 2 false”]

“cond 2 true”

Regards,

Jason

Jason Fealy | Kx Engineer | Kx | +1 (718) 340-9791 | jfealy@kx.com

‘if’ conditionally evaluates all blocks after the condition and returns nothing

http://code.kx.com/wiki/Reference/if

$[;;…] is used for lazy evaluation of an if/elsif/elsif/…/else statement and returns the last evaluated block.
http://code.kx.com/wiki/Reference/DollarSign#cond