Comparison Operators

Prev Next

Introduction

Unlike other programming languages which define 6 comparison operators, B4P supports 8 comparison operators which can be combined with comparison options to make adjustments on the comparison rules:

  • < Less than
  • <= Less than or equal to
  • > Greater than
  • >= Greater than or equal to
  • = Equal to, expecting 1 or more operands separated by comma (comparing with a selection of multiple values separated with commas) and/or ranges.
  • <> Not equal to, expecting 1 or more operands separated by comma (comparing with a selection of multiple values separated with commas) and/or ranges.
  • == strictly Equal to, expecting 1 operand only
  • != strictly not equal to, expecting 1 operand only

Comparisons with b>= and >< are more tolerant in comparing numbers and strings containing numbers, and also support wildcard symbols on the right-hand operands if they are defined as softquoted strings (e.g. text inside single quotation marks).

Attention! = and <> may Hijack Parameters

Attention: As the two comparison operators = and >< expect 1 or more operands on the right hand side, you may risk situations where the comparison hijacks the remaining comma-separated values on the right-hand-side typically encountered in function parameters and values in parameter sets. To avoid this, put the comparison into parentheses, or use the == and != symbols.

Examples 02

  b[] = 12;
  // Want to compare b[] == 9 and include the result in the parameter set between 'false' and 12
  // Good luck!

  a[0] = { 3, 6, false, b[] =  9 , 12, 15 }; // Comparison has hijacked the last two parameters
  a[1] = { 3, 6, false,(b[] =  9), 12, 15 }; // OK
  a[2] = { 3, 6, false, b[] == 9 , 12, 15 }; // OK

  for all variables( a[], b[] ) echo( b[] );

Output 02

{3,6,false,true}
{3,6,false,false,12,15}
{3,6,false,false,12,15}
Try it yourself: Open LAN_Features_comparison_operators.b4p in B4P_Examples.zip. Decompress before use.