Part 3 Precedences of operators

Part 3 Precedences - Precedences of operators You know that multiplication takes precedence over addition e.g the expression 5 4 3 is evaluated as

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

View Full Document Right Arrow Icon
Precedences of operators You know that multiplication * takes precedence over addition +, e.g. the expression 5 + 4 * 3 is evaluated as if it were parenthesized like this: 5 + (4 * 3). Mathematics has conventions for precedences of operators in order to reduce the number of parentheses required in writing complex expressions. Some of these conventions are standard throughout the world —like * over +. Others are not. Below is a table of precedences for Java operators. Refer to this table, or the one on p. 227 of Gries/Gries, if you forget the precedences. Table of operator precedences ORDER OPERATORS EXAMPLES Highest Unary ops: + – ++ –– ! Binary arithmetic ops. * / % Binary arithmetic ops. + – Arithmetic relations: < > <= >= Equality relations: == != Logical and: Lowest Logical or: || For example, the expression can be easily rewritten as n != 0 && 10/n > 2 because relational operators have precedence over &&. Keep redundant parentheses to a minimum (but don't sacrifice clarity)
Background image of page 1

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

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

This note was uploaded on 07/07/2008 for the course CS 101 taught by Professor Gries during the Spring '08 term at Cornell University (Engineering School).

Page1 / 2

Part 3 Precedences - Precedences of operators You know that multiplication takes precedence over addition e.g the expression 5 4 3 is evaluated as

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

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