Operator Precedence and Parentheses

Prev Next

Overview

B4P applies meaningful operator precedence rules when carrying out calculations with unary and binary operators. For calculation examples, do the multiplications and divisions first, and then the additions and subtractions (briefly said in German: Punktrechnung vor Strichrechnung). Use parentheses to influence the precedence rules.

Precedence Symbols Description
1 (highest) +, -, ~, ! All unary operators
2 *, / Arithmetic operators: multiplication and division
3 +, - Arithmetic operators: addition and subtraction
4 =, ==, <>, !=, >, >, <, <= Comparison operators
5 (lowest) &, | Logical operators

The operators are described in the following section.

Parentheses

Parentheses can be used to overrule the operator precdences.

Examples
echo(  3 + 10 *2 + 100*4 ); // 423
echo( (3 + 10)*2 + 100*4 ); // 426

echo( 3 + 2 >= 2 + 2 );     // True
echo( 3 + (2 >= 2) + 2 );   // 6 (True in middle converts to 1)
423
426
true
6
Try it yourself: Open LAN_Features_Operator_Precedence.b4p in B4P_Examples.zip. Decompress before use.

See also

Binary Operators
Unary Operators