26.07.2013 Views

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

Chapter 4 Control Structures: Part 1 159<br />

Software Engineering Observation 4.3<br />

Just as a block can be placed anywhere a single statement can be placed, it is also possible<br />

<strong>to</strong> have no statement at all (i.e., the empty statement in such places). The empty statement is<br />

represented by placing a semicolon (;) where a statement would normally be. 4.3<br />

Common <strong>Program</strong>ming Error 4.3<br />

Placing a semicolon after the condition in an if structure leads <strong>to</strong> 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). 4.3<br />

Good <strong>Program</strong>ming Practice 4.5<br />

Some programmers prefer <strong>to</strong> type the beginning and ending braces of blocks before typing<br />

the individual statements within the braces. This practice helps avoid omitting one or both of<br />

the braces. 4.5<br />

In this section, we have introduced the notion of a block. A block may contain declarations<br />

(as does the body of main, for example). The declarations in a block commonly are<br />

placed first in the block before any action statements occur, but declarations may also be<br />

intermixed with action statements.<br />

4.7 The while Repetition Structure<br />

A repetition structure allows the programmer <strong>to</strong> specify that an action is <strong>to</strong> 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 structure. The body of the while structure may be a single statement<br />

or a block. 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 <strong>Program</strong>ming Error 4.4<br />

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

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

will never terminate—an error called an infinite loop. 4.4<br />

Common <strong>Program</strong>ming Error 4.5<br />

Spelling the keyword while with an uppercase W, as in While, is a syntax error. (Remember<br />

that <strong>Java</strong> is a case-sensitive language.) All of <strong>Java</strong>’s reserved keywords, such as while,<br />

if and else, contain only lowercase letters. 4.5<br />

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

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

<strong>to</strong> 2. When the following while structure finishes executing, product contains<br />

the result:<br />

© Copyright 1992–2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01

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

Saved successfully!

Ooh no, something went wrong!