Main Content

mp_0010: Precedence of Operators in Arithmetic Expressions

Since R2024b

Guideline Publication

Control Algorithm Modeling Guidelines - Using MATLAB®, Simulink®, and Stateflow®

Sub ID Recommendations

  • NA-MAAB — a

  • JMAAB — a

MATLAB Versions

All

Rule

Sub ID a

When an expression contains operators with different precedence levels, parentheses shall be used to clarify the order of operations.

Example — Correct

foo = (bar * baz) + qux;
foo = bar + (baz * qux);
(bar && baz) || (qux && quux);

When the operators have the same precedence, parentheses are unnecessary.

foo = bar + baz - qux;
Example — Incorrect

foo = bar * baz + qux;
foo = bar + baz * qux;
bar && baz || qux && quux;

Rationale

Sub ID a:

  • Readability could lead to unintended functionality.

Verification

Model Advisor check: Check for operator precedence (Simulink Check)

Last Changed

R2024b

Version History

Introduced in R2024b