Lecture04 - Expressions Data Conversion and Input Expressions Operators and Precedence Assignment Operators Data Conversion Input and the Scanner

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Expressions, Data Conversion, and Input Expressions Operators and Precedence Assignment Operators Data Conversion Input and the Scanner Class Reading for this class: L&L, 2.4-2.6, App D
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Expressions An expression is a combination of one or more operators and operands Arithmetic expressions compute numeric results and make use of the arithmetic operators: If either or both operands used by an arithmetic operator are floating point, then the result is a floating point Addition + Subtraction - Multiplication * Division / Remainder %
Background image of page 2
3 Division and Remainder If both operands to the division operator ( / ) are integers, the result is an integer (the fractional part is discarded) The remainder operator (%) returns the remainder after dividing the second operand into the first 14 / 3 equals 8 / 12 equals 4 0 14 % 3 equals 8 % 12 equals 2 8
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Operator Precedence Operators can be combined into complex expressions result = total + count / max - offset; Operators have a well-defined precedence which determines the order in which they are evaluated Multiplication, division, and remainder are evaluated prior to addition, subtraction, and string concatenation Arithmetic operators with the same precedence are evaluated from left to right, but parentheses can be used to force the evaluation order See Appendix D for a more complete list of operators and their precedence.
Background image of page 4
5 Operator Precedence What is the order of evaluation in the following expressions? a + b + c + d + e 1 4 3 2 a + b * c - d / e 3 2 4 1 a / (b + c) - d % e 2 3 4 1 a / (b * (c + (d - e))) 4 1 2 3
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 Expression Trees The evaluation of a particular expression can be shown using an expression tree The operators lower in the tree have higher precedence for that expression a + (b – c) / d a + / - d b c
Background image of page 6
7 Assignment Revisited The assignment operator has a lower
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/25/2010 for the course CS 110 taught by Professor Wilson during the Spring '10 term at University of Massachusetts Boston.

Page1 / 23

Lecture04 - Expressions Data Conversion and Input Expressions Operators and Precedence Assignment Operators Data Conversion Input and the Scanner

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online