#include "MusimatTutorial.h"Go to the source code of this file.
Functions | |
| MusimatTutorialSection (B0112) | |
| MusimatTutorialSection | ( | B0112 | ) |
Definition at line 2 of file B0112.cpp.
{
Print("*** B.1.12 Operator Precedence and Associativity ***");
/*****************************************************************************
B.1.12 Operator Precedence and Associativity
In the expression a * x + b * y, what is the order in which the operations are carried out? By
the standard rules of mathematics, we should first form the products a*x and b*y, then
sum the result. So multiplication has higher precedence than addition. The natural precedence of
operations can be overridden by the use of parentheses. For example, a * (x + b) * y forces the
summation to occur before the multiplications.
In the expression a + b + c, we first add a to b, then add the result to c, so the associativity
of addition is left to right. We could express left-to-right associativity explicitly like this:
(((a) + b) + c).
The rules of precedence and associativity in programming languages can be complicated, but
the programming examples in this section use the following simplified rules.
Expressions are evaluated from left to right, except
o Multiplications and divisions are performed before additions and subtractions.
o All arithmetic operations (+, -, *, /) are performed before logical operations (And, Or,
==, <, >).
o Parentheses override the above precedence rules.
For details, see section B.3.
*****************************************************************************/
Integer a = 2, x = 3, b = 4, y = 5;
Print("a * x + b * y = ", a * x + b * y); // prints 26
Print("a * (x + b) * y = ", a * (x + b) * y); // prints 70
}
1.7.2