// Andrew Martin // Demonstration of precedence and associativity // of arithmetic and Boolean operators in C++ #include using namespace std; float y = 0;; int z = 0; float returnFloatAndIncrement () { y++; return 5.0; } int returnIntAndIncrement () { z++; return 5; } int main() { float a, b, c; int m, n, o; bool d, e, f; //Begin Associativity Tests cout << "Precedence tests:\n"; cout << "Test - Addition and Multiplication:\n"; a = 2 + 3 * 4; b = 2 + (3 * 4); c = (2 + 3) * 4; if (a == b) cout << "\tMultiplication has precedence.\n"; else if (a == c) cout << "\tAdditions has precedence.\n"; cout << "Test - Subtraction and Division:\n"; a = 2 - 3 / 4; b = 2 - (3 / 4); c = (2 - 3) / 4; if (a == b) cout << "\tDivision has precedence.\n"; else if (a == c) cout << "\tSubtraction has precedence.\n"; cout << "Test - Addition and Modulo:\n"; a = 2 + 3 % 4; b = 2 + (3 % 4); c = (2 + 3) % 4; if (a == b) cout << "\tModulo has precedence.\n"; else if (a == c) cout << "\tAdditions has precedence.\n"; // The hardest part here was finding situations that will // deliver correct results for boolean testing. cout << "Test - AND and OR:\n"; d = false && false || true; e = false && (false || true); f = (false && false) || true; if (d == e) cout << "\tOR has precedence.\n"; else if (d == f) cout << "\tAND has precedence.\n"; cout << "Test - EQUAL-TO and OR:\n"; d = false == true || true; e = false == (true || true); f = (false == true) || true; if (d == e) cout << "\tOR has precedence.\n"; else if (d == f) cout << "\tEQUAL-TO has precedence.\n"; cout << "Test - NOT-EQUAL and AND:\n"; d = true != false && false; e = true != (false && false); f = (true != false) && false; if (d == e) cout << "\tAND has precedence.\n"; else if (d == f) cout << "\tNOT-EQUAL has precedence.\n"; //Begin Associativity Tests // For these tests, the function returnIntAndIncrement() // and returnFloatAndIncrement() increments y by 1 and returns // the value of 5 and 5.0 respectively. // This allows determination of which side is evaluated first. // Although the text shows Addition and Multiplication as left // associative, but for some reason these tests below evaluate // the right side first. cout << "\nAssociativity Tests:\n"; cout << "Test - Addition:\n"; z = 1; m = z + returnIntAndIncrement(); n = 6; o = 7; if (m == n) cout << "\tAddition is left associative.\n"; else if (m == o) cout << "\tAddition is right associative.\n"; cout << "Test - Subtraction:\n"; y = 8; a = y - returnIntAndIncrement(); b = 3; c = 4; if (a == b) cout << "\tSubtraction is left associative.\n"; else if (a == c) cout << "\tSubtraction is right associative.\n"; cout << "Test - Multiplication:\n"; z = 8; m = z * returnIntAndIncrement(); n = 40; o = 45; if (m == n) cout << "\tMultiplication is left associative.\n"; else if (m == o) cout << "\tMultiplication is right associative.\n"; cout << "Test - Division:\n"; y = 20.0; a = y / returnFloatAndIncrement(); b = 4.0; c = 4.2; if (a == b) cout << "\tDivision is left associative.\n"; else if (a == c) cout << "\tDivision is right associative.\n"; cout << "Test - Modulo:\n"; z = 8; m = z % returnIntAndIncrement(); n = 3; o = 4; if (m == n) cout << "\tModulo is left associative.\n"; else if (m == o) cout << "\tModulo is right associative.\n"; return 0; }