select if, pick if

Prev Next

Function Names

select if, pick if

Description

This function selects and returns the 2nd function parameter if the 1st parameter is 'true', otherwise the 3rd function parameter is returned.

select will calculate all function parameters.
pick will calculate the 1st parameter and then either the 2nd (if true) or 3rd function parameter (if false). In C/C++, the function works similar to the following trinary operator: ? ... ? ... .

Call as: procedure or function

Restrictions

Indirect parameter passing is disabled

Parameter count

3

Parameters

No.TypeDescription
1
input
boolean Boolean value

2
input
all types Selected value if true

This expression will be always calculated. It will be returned if 'true' is provided in the 1st function parameter.

Alt. 2
code
expression
:string
Selected expression if true

Applicable to pick if: This expression will be calculated and the value returned if 'true' is provided in the 1st function parameter. Otherwise, calculating this parameter will be skipped.

3
input
all types Selected value if false

This expression will be always calculated. It will be returned if 'false' is provided in the 1st function parameter.

Alt. 3
code
expression
:string
Selected expression if false

Applicable to pick if: This expression will be calculated and the value returned if 'false' is provided in the 1st function parameter. Otherwise, calculating this parameter will be skipped.

Return value

TypeDescription
all types Selected value

Examples

       a[] = 1; b[] = 2;

       echo("Function 'select if':");
       print( select if( true, a[]++, b[]++ ) ); // Both a[] and b[] are incremented
       echo( "   a[] = ", a[], " b[] = ", b[] );

       echo;

       a[] = 1; b[] = 2;

       echo("Function 'pick if':");
       print( pick if( false, a[]++, b[]++ ) ); // Only b[] is incremented
       echo( "   a[] = ", a[], " b[] = ", b[] );

Output

Function 'select if':
1   a[] = 2 b[] = 3

Function 'pick if':
2   a[] = 1 b[] = 3
Try it yourself: Open LIB_Function_select_if.b4p in B4P_Examples.zip. Decompress before use.

See also

select ifs
pick ifs
select
pick