23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

% the modulo operator<br />

This last operator, modulo, is also known as the "rema<strong>in</strong>der" operator, because it<br />

is the rema<strong>in</strong>der left after an <strong>in</strong>teger division. We often use "mod" to denote the<br />

modulo operator, <strong>and</strong> we def<strong>in</strong>e it formally as<br />

n mod m = r,<br />

such that<br />

n = mq + r,<br />

for an <strong>in</strong>teger q <strong>and</strong> 0 ≤ r < n.<br />

<strong>Java</strong> also provides a unary m<strong>in</strong>us (−), which can be placed <strong>in</strong> front of an arithm<br />

etic expression to <strong>in</strong>vert its sign. Parentheses can be used <strong>in</strong> any expression to<br />

def<strong>in</strong>e the order of evaluation. <strong>Java</strong> also uses a fairly <strong>in</strong>tuitive operator precedence<br />

rule to determ<strong>in</strong>e the order of evaluation when parentheses are not used. Unlike<br />

C++, <strong>Java</strong> does not allow operator overload<strong>in</strong>g.<br />

Increment <strong>and</strong> Decrement Operators<br />

Like C <strong>and</strong> C++, <strong>Java</strong> provides <strong>in</strong>crement <strong>and</strong> decrement operators. Specifically,<br />

it provides the plus-one <strong>in</strong>crement (++) <strong>and</strong> decrement (−−) operators. If such an<br />

operator is used <strong>in</strong> front of a variable reference, then 1 is added to (or subtracted<br />

from) the variable <strong>and</strong> its value is read <strong>in</strong>to the expression. If it is used after a<br />

variable reference, then the value is first read <strong>and</strong> then the variable is <strong>in</strong>cremented<br />

or decremented by 1. So, for example, the code fragment<br />

<strong>in</strong>t i = 8;<br />

<strong>in</strong>t j = i++;<br />

<strong>in</strong>t k = ++i;<br />

<strong>in</strong>t m = i−−;<br />

<strong>in</strong>t n = 9 + i++;<br />

assigns 8 to j, 10 to k, 10 to m, 18 to n, <strong>and</strong> leaves i with value 10.<br />

Logical Operators<br />

<strong>Java</strong> allows for the st<strong>and</strong>ard comparison operators between numbers:<br />

< less than<br />

43

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!