Operator Precedence and Parentheses

Prev Next

Overview

B4P applies commonly practiced operator precedence rules when carrying out calculations with unary and binary operators. For example, multiplications and divisions are performed first before continuing with additions and subtractions.

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