Assignment operators

Prev Next

Introduction

Similar to C/C++, B4P supports assignment operators as listed in the table below. An assignment operator such as a[]+=3; is equivalent to a[]=a[]+3, making use of the same type checking and conversion rules. Spaces between the assignment operator and the equal sign are allowed. The execution performance of assignment operators is faster than conventional formulation because fewer language elements (symbols, names, values) are interpreted, e.g. from 9 (a[]=a[]+3) to 6 (a[]+=3).

Operator Examples Equivalent traditional formulation
+= a[] += 3;
a[] += ' km/h';
a[] = a[] + 3;
a[] = a[] + ' km/h';
-= a[] -= 4;
a[] -= {'1','2'};
a[] = a[] - 4;
a[] = a[] - { '1','2' };
*= a[] *= 5;
a[] = a[] * 5;
/= a[] /= 2;
a[] /= '.';
a[] = a[] / 2;
a[] = a[] / '.';
&= a[] &= b[]; a[] = a[] & b[];
|= a[] |= b[]; a[] = a[] | b[];
== = a[] == = b[]; a[] = a[] == b[];
!= = a[] != = b[]; a[] = a[] != b[];

In the last two rows, the symbols may be written together, but have been shown separately for better clarity.

In the destination variable is not existing yet and an assignment operator is applied, then the destination variable will be initialized implicitly with following values:

Type Value Examples Equivalen traditional formulation
Numeral 0 a[] += 5; a[] = 0; a[] = a[] + 5;
String '' (blank) a[] += 's'; a[] = ''; a[] = a[] + 's';
Date (blank date) a[] += date(today); a[] = date(''); a[] = a[] + date(today);
Boolean false a[] |= b[]; a[] = false; a[] = a[] | b[];
parameter set {} (empty set) a[] |= {1,2}; a[] = {}; a[] = a[] | {1,2};

  // Implicit initialization cases

  a[0] += 3;
  a[1] += 'Hello';
  a[2] += date("2020-10-12");
  a[3] |= false;
  a[4] |= { 1,2,3 };
  a[5] += true;   // Arithmetic operator converts true to 1 first
  a[6] = ''; a[6] -= 1; // a[] = '' - 1 is legitimate because arithmetic calculation
  // of numbers with blank strings is valid where blanks are treated as zero.

  // Doing some calculations

  a[0] *= 2;      // 3 --> 6
  a[1] -= llo;    // Hello --> He
  a[2] += 1;      // Next day
  a[3] |= true;   // true
  a[4] *= 2;      // { 1,2,3, 1,2,3 }
  a[5] /= 2;      // 1 -> 0.5

  for all variables( a[], x[] ) echo ( x[] );
6
He
2020-10-13
true
{1,2,3,1,2,3}
0.5
-1
Try it yourself: Open LAN_Features_Assignment_operator.b4p in B4P_Examples.zip. Decompress before use.

Assignment operators on table cells

Using assignment operators on tables is supported to a limited extent since the type of the destination is either a string or numeral (legitimate positive or negative number with or without decimal point recognized) unless the table has been configured differently using the table configure() function.

  table create ( a );

  [a:0,0] += 1;
  [a:0,1] += 'Hello';
  [a:0,1] -= 'llo';
  [a:0,1] += 'y'; // Hey
  [a:0,2] = true; // converts to string
  [a:0,2] += ' false'; // 'true false'

  table list(a);
    0 : 1         
    1 : Hey       
    2 : true false

Try it yourself: Open LAN_Features_Assignment_operator_01.b4p in B4P_Examples.zip. Decompress before use.

See also

Deep assignment operators