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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

2008934301<br />

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

Common Programming Error 14.3<br />

Placing a semicolon after the condition in an if structure leads to a logic error in singleselection<br />

if structures and a syntax error in double-selection if structures (if the if part<br />

contains a nonempty body statement). 14.3<br />

Good Programming Practice 14.4<br />

Some programmers prefer to type the beginning and ending braces of compound statements<br />

before typing the individual statements within the braces. This helps avoid omitting one or<br />

both of the braces. 14.4<br />

14.7 The while Repetition Structure<br />

A repetition structure allows the programmer to specify that an action is to be repeated<br />

while some condition remains true. The pseudocode statement<br />

While there are more items on my shopping list<br />

Purchase next item and cross it off my list<br />

describes the repetition that occurs during a shopping trip. The condition “there are more<br />

items on my shopping list” may be true or false. If it is true, then the action “Purchase next<br />

item and cross it off my list” is performed. This action will be performed repeatedly while<br />

the condition remains true. The statement(s) contained in the while repetition structure constitute<br />

the body of the while. The while structure body may be a single statement or a compound<br />

statement. Eventually, the condition will become false (when the last item on the<br />

shopping list has been purchased and crossed off the list). At this point, the repetition terminates,<br />

and the first pseudocode statement after the repetition structure is executed.<br />

Common Programming Error 14.4<br />

Not providing in the body of a while structure an action that eventually causes the condition<br />

in the while to become false is a logic error. Normally, such a repetition structure will<br />

never terminate—an error called an “infinite loop.” Browsers handle infinite loops differently.<br />

For example, Internet Explorer allows the user to terminate the script containing the<br />

infinite loop. 14.4<br />

Common Programming Error 14.5<br />

Spelling the keyword while with an uppercase W as in While (remember that <strong>JavaScript</strong><br />

is a case-sensitive language) is a syntax error. All of <strong>JavaScript</strong>’s reserved keywords, such<br />

as while, if and else, contain only lowercase letters. 14.5<br />

As an example of a while structure, consider a program segment designed to find the<br />

first power of 2 larger than 1000. Suppose variable product has been initialized to 2.<br />

When the following while structure finishes executing, product contains the result:<br />

var product = 2;<br />

while ( product

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

Saved successfully!

Ooh no, something went wrong!