Ad-hoc operators are features which apply an immediate action on the variable or table field referenced. The increment and decrement operators (++ and --) common in C/C++, Java and Python are just two of a variety of ad-hoc operators. Ad-hoc operators can be applied in following ways:
++ | Increment |
-- | Decrement |
++(n) | Increment by specified numeral or numeric expression (negative values will obviously decrement the target value) |
--(n) | Decrement by specified numeral or numeric expression (negative values will obviously increment the target value) |
**(n) | Apply scaling factor, with numeral or numeric expression in parentheses. An equivalent symbol for division does not exist. Use a reciprocal value in combination with the scaling factor instead. |
Note: The two consecutive characters need to be written together without spaces. Otherwise they are recognized as plus, minus and multiplication symbols. False example: c[] = - - a[]; where the value of a[] is negated twice and assigned to c[]. Ad-hoc operators are allowed on target value with following types:
Ad-hoc operators can be applied on variable as well as table cells. However following restrictions apply:
B4P provides following ad-hoc operators;
Ad-hoc operators can also be cascaded, e.g. a[]++ ++, a[] **(3) ++. Following restriction applies: All scaling operations must
happen before ++ and/or -- operators. Not valid: a[] ++ **(3). However ++a[]**(3) is OK.
a[] = 10;
b[] = 20;
d[] = 30;
echo("Demonstrate ad-hoc operations in right-hand expressions:");
// 10 11 9 11
echo( a[]++, ' ', a[]--, ' ', --a[], ' ', ++ ++a[] );
// 22 22 -> 66 6
echo( **(2)a[], ' ', a[]**(3), ' ', **(1/11)a[] );
x[] = --(2) a[]++; // Decrement a by 2, assign, then increment by 1. Result: 4, 5
echo( x[], ' ', a[]);
echo(new line, "Demonstrate ad-hoc operations in left-hand expressions:");
e[]++ = b[]; // becomes 21
f[]**(2) = 10; // becomes 20
echo( e[], ' ', f[]) ;
echo(new line, "Demonstrate stand-alone ad-hoc operations:");
d[]++; // Simple one, 31.
**(2)e[]--; // Double 21 to 42, subract by 1 becomes 41.
echo( d[], ' ', e[]) ;
echo(new line, "Demonstrate stand-alone ad-hoc operations in tables:");
table create ( t ); // Ad-hoc operators applied on table cells
[t:0,0]++; // Table entry was blank. Arithmetics interpret blanks as zero; Contains 1.
[t:0,0]**(3); // Multiplied by 3.
echo( [t:0,0] ); // 3
echo(new line, "Demonstrate stand-alone ad-hoc operations with dates:");
d[] = date( "2020-08-01 18:30:00" );
echo( --d[] ); // Previous day
echo( ++(0.5)d[] ); // Next day, 06:30
echo(new line, "Demonstrate stand-alone ad-hoc operations with sets:");
a[] = { 1, 2, 5, 10, { 20, 50 } };
a[]++;
echo(a[]);
Demonstrate ad-hoc operations in right-hand expressions:
10 11 9 11
22 22 6
4 5
Demonstrate ad-hoc operations in left-hand expressions:
21 20
Demonstrate stand-alone ad-hoc operations:
31 41
Demonstrate stand-alone ad-hoc operations in tables:
3
Demonstrate stand-alone ad-hoc operations with dates:
2020-07-31 18:30:00
2020-08-01 06:30:00
Demonstrate stand-alone ad-hoc operations with sets:
{2,3,6,11,{21,51}}