00001 #include "MusimatTutorial.h" 00002 MusimatTutorialSection(B0400) { 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 00066 00068 /* $Revision: 1.3 $ $Date: 2006/09/05 08:03:08 $ $Author: dgl $ $Name: $ $Id: B0400.cpp,v 1.3 2006/09/05 08:03:08 dgl Exp $ */ 00069 // The Musimat Tutorial © 2006 Gareth Loy 00070 // Derived from Chapter 9 and Appendix B of "Musimathics Vol. 1" © 2006 Gareth Loy 00071 // and published exclusively by The MIT Press. 00072 // This program is released WITHOUT ANY WARRANTY; without even the implied 00073 // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00074 // For information on usage and redistribution, and for a DISCLAIMER OF ALL 00075 // WARRANTIES, see the file, "LICENSE.txt," in this distribution. 00076 // "Musimathics" is available here: http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&tid=10916 00077 // Gareth Loy's Musimathics website: http://www.musimathics.com/ 00078 // The Musimat website: http://www.musimat.com/ 00079 // This program is released under the terms of the GNU General Public License 00080 // available here: http://www.gnu.org/licenses/gpl.txt 00081