Presentation is loading. Please wait.

Presentation is loading. Please wait.

CIS 234: Order of Operations, Shortcut & Other Operators Dr. Ralph D. Westfall February, 2004.

Similar presentations


Presentation on theme: "CIS 234: Order of Operations, Shortcut & Other Operators Dr. Ralph D. Westfall February, 2004."— Presentation transcript:

1 CIS 234: Order of Operations, Shortcut & Other Operators Dr. Ralph D. Westfall February, 2004

2 Some Things to Think About order of operations what gets calculated 1 st ? type casting changing data from one type of variable to a different type shortcut operators doing the same calculations with less typing

3 Order of Operations order in which calculations and other operations are performed by language 2 + 4 / 2 // is result 3 or 4? operations that are done first have "higher precedence" pp. 45-46, 188 "Please excuse my dear aunt Sally"

4 Order of Operations - 2 operations within parentheses are always done first inner before outer parentheses e.g., (5 * (2 + 3)) ("please") method calls next (lower precedence) Math.pow(2, 3) // Java's exponentiation // == 2*2*2 ("excuse")

5 Order of Operations - 3 following all have the same precedence: "unary" minus unary minus doesn't subtract from anything value = -5 ; value = - x; // reverses sign increment or decrement: ++ and -- type casting (changes data type) value = (double) 5 / 2 ;

6 Type Casting can change data type of a variable by putting a different data type in front in parentheses e.g., (int), (char), (float) can also put parentheses around an expression, and a cast before that, to change the data type of the result double a = (int) (3.0 / 2); // a? // how many conversions in above line?

7 Order of Operations - 4 then multiply ("my"), regular division ("dear"), and modulus operations *, /, % then addition ("aunt"), and subtraction ("Sally") +, -

8 Order of Operations - 5 then comparison operations (in order), >= // highest among these ==, != && (AND) || (OR) // lowest among these then assignment (equals) = e.g., height = 60; or shortcut assignment e.g., x += y; p. 188

9 Practice evaluate the following: x = 3 * 5 + 4 / 2 – Math.min(3, 8) y = (9 + 2) == (7 + 4) z = (double) 3 / 2 ; //unifying type? a = (float) 5.0 / 2 ; //a decimal type b = (int) (7 / 2.0) ;

10 Shortcut Operators way to write code with less typing result is exactly same code may actually run faster examples ++a; // exactly same as a = a + 1; ++a; 4 keystrokes (including semicolon) a=a+1; 6 keystrokes a = a + 1; 10 keystrokes (4 spaces)

11 Addition and Subtraction ++ is the "increment operator" adds 1 -- is the "decrement operator" subtracts 1 both are "unary" operators, because they only work on 1 variable contra: a + b, c = d, e == f, etc.

12 Prefix versus Postfix prefix means put the operator before the variable e.g., ++a, -- b increments or decrements a variable before it is used in an expression a = 2; x = ++a; now x is 3, a is 3

13 Prefix versus Postfix - 2 postfix means put the operator after the variable e.g., a--, b++ decrements or increments a variable after it is used in an expression a = 2; x = a++; now x is 2, a is 3

14 Practice a=4; b=3; x=++a;// x? a? y=a -- ;// y? a? x= ++a + b -- ;// x? a? b? y= a -- - (++b);// y? a? b?

15 Other Shortcut Operators can add, subtract, multiply, divide, or get modulus not limited to add or subtract just 1 a += 2; // same as a = a + 2; a -= 2; // same as a = a - 2; a *= 2; // same as a = a * 2; a /= 2.0;// same as a = a / 2.0;

16 Other Shortcut Operators - 2 modulus = remainder after integer division ("clock arithmetic") use % symbol 5 % 2 == 1 if num % 2 == 0, num is an even number shortcut operator for modulus a %= 2; // same as a = a % 2;

17 Practice a=4; b=5; // a? a += 5; // a? b -= 4; // b? b *= 4; // b? a %= ++b; // a? b?

18 Shortcuts in Totals total = 0.00; sale = 3.00; total += sale; /* would work well in a loop: read a line from a file into sale variable, add it onto total, read another line into sale, etc. */


Download ppt "CIS 234: Order of Operations, Shortcut & Other Operators Dr. Ralph D. Westfall February, 2004."

Similar presentations


Ads by Google