23.07.2013 Views

JavaScript/JScript: Control Structures I - Pearson Learning Solutions

JavaScript/JScript: Control Structures I - Pearson Learning Solutions

JavaScript/JScript: Control Structures I - Pearson Learning Solutions

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.

2008934301<br />

Chapter 14 <strong>JavaScript</strong>/<strong>JScript</strong>: <strong>Control</strong> <strong>Structures</strong> I 433<br />

It is important to note here that when incrementing or decrementing a variable in a<br />

statement by itself, the preincrement and postincrement forms have the same effect and the<br />

predecrement and postdecrement forms have the same effect. It is only when a variable<br />

appears in the context of a larger expression that preincrementing the variable and postincrementing<br />

the variable have different effects. Predecrementing and postdecrementing<br />

behave similarly.<br />

Common Programming Error 14.10<br />

Attempting to use the increment or decrement operator on an expression other than an lvalue<br />

is a syntax error. An lvalue is a variable or expression that can appear on the left side of an<br />

assignment operation. For example, writing ++(x + 1) is a syntax error because (x + 1)<br />

is not an lvalue. 14.10<br />

The chart in Fig. 14.15 shows the precedence and associativity of the operators introduced<br />

up to this point. The operators are shown from top to bottom in decreasing order of<br />

precedence. The second column describes the associativity of the operators at each level of<br />

precedence. Notice that the conditional operator (?:), the unary operators increment (++)<br />

and decrement (--) and the assignment operators =, +=, -=, *=, /= and %= associate from<br />

right to left. All other operators in the operator precedence chart of Fig. 14.15 associate<br />

from left to right. The third column names the groups of operators.<br />

14.13 A Note on Data Types<br />

Unlike its predecessor languages C, C++ and Java, <strong>JavaScript</strong> does not require variables to<br />

have a type before they can be used in a program. A variable in <strong>JavaScript</strong> can contain a<br />

value of any data type, and in many situations <strong>JavaScript</strong> automatically converts between<br />

values of different types for you. For this reason, <strong>JavaScript</strong> is referred to as a loosely typed<br />

language.<br />

When a variable is declared in <strong>JavaScript</strong> but is not given a value, that variable has an<br />

undefined value. Attempting to use the value of such a variable is normally a logic error.<br />

When variables are declared, they are not assigned default values unless specified otherwise<br />

by the programmer. To indicate that a variable does not contain a value, you can<br />

assign the value null to the variable.<br />

Operators Associativity Type<br />

() left to right parentheses<br />

++ -- right to left unary<br />

* / % left to right multiplicative<br />

+ - left to right additive<br />

< >= left to right relational<br />

== != left to right equality<br />

?: right to left conditional<br />

= += -= *= /= %= right to left assignment<br />

Fig. 14.15 Precedence and associativity of the operators discussed so far.<br />

e-Business and e-Commerce: How to Program, by Harvey M. Deitel, Paul J. Deitel, and Tem R. Nieto. Published by Prentice Hall.<br />

Copyright © 2001 by <strong>Pearson</strong> Education, Inc.

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

Saved successfully!

Ooh no, something went wrong!