/* DemoPrecedence.cpp CIS 150 5/29/2008 David Klick This program demonstrates operator precedence. Multiplication, division, and modulus will be calculated before addition and subtraction. Parentheses can be used to override the normal precedence. */ #include using std::cin; using std::cout; using std::endl; void cont(); int main() { // declare and initialize variables int x = 3; int y = 5; // declare more variables using those above int result1 = x + y / 2; // Note: 5.5 truncated to 5 int result2 = (x + y) / 2; int result3 = x + y * 2; int result4 = (x + y) * 2; int result5 = x + y % 8; int result6 = (x + y) % 8; // display results of calculations cout << result1 << '\n'; cout << result2 << '\n'; cout << result3 << '\n'; cout << result4 << '\n'; cout << result5 << '\n'; cout << result6 << '\n'; cont(); return 0; } void cont() { if (cin.rdbuf()->sungetc() != -1 && cin.get() != '\n') cin.ignore(80,'\n'); cout << "Press Enter to continue..."; cin.get(); return; } /* Sample output: 5 4 13 16 8 0 */