Expressions and Order of Operations Definition An expression

  • Slides: 12
Download presentation
Expressions and Order of Operations Definition: An expression is a sequence of operators and

Expressions and Order of Operations Definition: An expression is a sequence of operators and operands • Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times symbol on the keyboard) – Java has many other operators (Next slide) • Operands – Can be a literal (A constant that we don’t have to declare) – Can be a variable – Can be a call to a method call that returns a value (Ex: int x = 3 + Math. round(4. 4); ) • Order of operations (The text has a complete list) – Multiply and division happen before add and subtract – Use parentheses to alter the order Important Note: Equal does NOT mean equal, it means assign

Other Operators in Java Definition: Precedence is a number indicating which operators get applied

Other Operators in Java Definition: Precedence is a number indicating which operators get applied first • Prefix ++: Add one, very high precedence int x = 3; int y = ++x; // x and y becomes a 4. • Postfix ++: Add one, very low precedence Int x = 3; int y = x++; // x becomes 4, y becomes 3. • +=, -=, *=, /=: Adjust the previous value int x = 3; x += 4; // x becomes 7 Int x = 6; x /= 5; // x becomes 1 • %: Compute the remainder int x = 7 % 4; // x becomes a 3 (remainder of 7/4) int x = 7; x %=4; // x becomes 3 (remainder of x/4)

Assignment Operator • Java evaluates what is on the right of the equal sign,

Assignment Operator • Java evaluates what is on the right of the equal sign, and then stores into the variable on the left • You can have more than one assignment in a statement • Examples 1. x = 3 + 4; // Add 3 + 4 and store the result in x 2. int x = 3; y = x + 4; // Add 4 to what’s in x and store in y. 3. x = x + 3; // Add 3 to x and store in x 4. x = y + Math. round(3. 4); // Round 3. 4 and add to y and then store in x. * Equal does not mean equal; it means assign * The call to the Math. round() method returns a value

Bitwise Operators &(and), |(or), ^(xor), !(not) Consider a byte: b = 10110100, c =

Bitwise Operators &(and), |(or), ^(xor), !(not) Consider a byte: b = 10110100, c = 00000110 1. byte d = b & c puts a 4 in d (one bit only if both corresponding bits on) 2. byte d = c | 5 puts a 7 in d (one bit if either of the corresponding bits on) 3. byte d = c ^ 5 puts a 3 in c (one bit if corresponding bits not equal) 4. ! Flips the bits (!b becomes 75) We’ll discuss these examples in class

Ternary operator • Most operators use two operands, one to the left of the

Ternary operator • Most operators use two operands, one to the left of the operator and one to the right (ex: a+b) • Java has one ternary operator – Example: (The following statement stores a 10 into x) int x = (5<6)? 10 : 20; – Example: int x = 5; System. out. println( (x++<6)? "hello" : "goodbye"); System. out. println((x<6)? "hello": "goodbye"); (The first line above prints "hello", second prints "goodbbye")

Logical operators And (&&), OR (||), Not (!) Int x = 3; double y

Logical operators And (&&), OR (||), Not (!) Int x = 3; double y = 3. 1, z = 3. 0; String s="abc", t="abcdef", u="abb" Examples 1. 2. 3. 4. 5. 6. boolean b = (x<y); boolean b = (x<y && x<=z); boolean b = (x>y || x<z); boolean b = !(x>y || y<z); boolean b = (s<t); boolean b = (s<u); Note: s<t and s<u will not work in practice because we are comparing object addresses and not contents Answers: true, false, true, false

Basic Control Structures All computer processing can be done this way Sequence Structure 1.

Basic Control Structures All computer processing can be done this way Sequence Structure 1. 2. 3. 4. 5. Do this Do that Do something else Do that again Do another thing Condition or Transfer Structure IF this THEN do that ELSE do the other thing Iteration or Loop • • • WHILE this true DO something UNTIL this false FOR many times DO something

Condition Statement Do one thing if an expression is true, and another if it

Condition Statement Do one thing if an expression is true, and another if it is false • Syntax (Java Grammar) If (boolean expression) { ** true part: any number of Java statements ** } else { ** false part: any number of java statements ** } • Notes 1. The braces are not needed if there is only one statement in the block 2. The else part is optional

Condition Statement Example // Convert military time to standard time // Assume hours has

Condition Statement Example // Convert military time to standard time // Assume hours has the current military hours String str; hours = hours %12; if (hours == 0) hours = 12; if (hours <=12) str = " a. m. "; else { str = " p. m. "; } // {} not required here System. out. println("It is " + hours + str); There is a bug in this logic; do you see it?

Nested If /* Assume month has the current month stored in it and years

Nested If /* Assume month has the current month stored in it and years has the current year */ int days; if (month == 9 || month == 4 || month == 6 || month == 11) days = 30; else { if (month == 2) { if (year %4 == 0) days = 29; else days = 28; } else days = 31; } System. out. println("month " + month + " has " + days + " days");

Another Example • Find maximum of five numbers if it is greater than 10

Another Example • Find maximum of five numbers if it is greater than 10 • Variables we will use are: first, second, third, fourth, fifth maximum = first; if (second > maximum) maximum = second; if (third > maximum) maximum = third; if (fourth > maximum) maximum = fourth; if (fifth > maximum) maximum = fifth; if (maximum > 10) System. out. println(maximum); else System. out. println("Maximum less than 10");

Review • What are three structures that we can use to program any application,

Review • What are three structures that we can use to program any application, no matter how complicated? • What are the following operators for (%, ? : , and !)? • What is the difference between x++ and ++x? • What is the difference between || and |? • What is the ternary operator? • What does the term precedence mean? • What is the difference between = and ==? • What is a condition statement? • When are braces needed in a condition statement? • What is a bitwise operator?