#include "MusimatTutorial.h"Go to the source code of this file.
Functions | |
| MusimatTutorialSection (B0400) | |
| MusimatTutorialSection | ( | B0400 | ) |
Definition at line 2 of file B0400.cpp.
00002 { 00003 Print("*** B.4 Operator Associativity and Precedence in Musimat ***"); 00004 /***************************************************************************** 00005 00006 B.4 Operator Associativity and Precedence in Musimat 00007 00008 To keep it simple, the Musimat expressions in this book are formatted to obey simple left-to-right 00009 evaluation. In fact, the rules are a little more complex because Musimat is basically C++ in 00010 sheep’s clothing. 00011 00012 Table B.2 00013 Operator Precedence and Associativity 00014 -------------------------------------------------------------------------------------------------------- 00015 Operator Associativity Description Examples 00016 ( ) left to right grouping a * (x+y) == ax + ay 00017 – right to left negation –3 == –1 * 3 00018 * / left to right multiplication and division a * b, a / b 00019 % left to right remainder after integer division 10 % 3 == 1, 12 % 3 == 0 00020 + – left to right addition and subtraction a + b, a – b 00021 < <= > >= left to right less-than, less-than-or-equal, a < b, a <= b 00022 greater-than, greater-than-or-equal a > b, a >= b 00023 == != left to right equal, not equal a == b, a != b 00024 And left to right logical AND False And False == False 00025 False And True == False 00026 True And False == False 00027 True And True == True 00028 Or left to right logical OR False Or False == False 00029 False Or True == True 00030 True Or False == True 00031 True Or True == True 00032 = right to left assignment a = b, a = b + c 00033 00034 00035 Associativity of operators is generally left to right, except for assignment and negation. For 00036 example the expression a = c = d assigns the value d to c, then assigns c to a, thereby making 00037 all three have equal value. 00038 00039 Table B.2 shows Musimat’s simplified operator precedence and associativity in order from 00040 highest to lowest. This precedence list is a shortened version derived from C and C++. Since you 00041 can’t effectively read or write computer programs unless you have memorized these rules of oper- 00042 ator precedence and associativity, experts recommend that you study these tables while you brush 00043 your teeth every night (Press et al. 1988, 23). 00044 00045 Warning: some expressions that might seem to have self-evident meaning can’t be expressed 00046 as such in C/C++ and so don’t work in Musimat either. Take the expression c > b > a, for exam- 00047 ple. You’d hope it would test whether b lies between a and c. Alas. Consider this example: 00048 00049 If (3 > 2 > 1) Print("True") Else Print("False") 00050 00051 It first evaluates (3 > 2), which it discovers is True, and replaces this expression with the integer 00052 1 (which stands for True in C++). It then evaluates the expression ( 1 > 1 ) which is False. Prob- 00053 ably not what we wanted. This example can be rewritten as follows: 00054 *****************************************************************************/ 00055 00056 If (3 > 2 And 2 > 1) 00057 Print("True"); 00058 Else 00059 Print("False"); 00060 00061 /***************************************************************************** 00062 which will print True. 00063 00064 *****************************************************************************/ 00065 }}
1.4.7