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.

204 Control Structures: Part 2 Chapter 5<br />

counter = counter + 1<br />

counter += 1<br />

++counter<br />

counter++<br />

are equivalent in the increment portion of the for structure. Many programmers prefer the<br />

form counter++, because a for structure increments its control variable after the body<br />

of the loop executes and placing ++ after the variable name increments the variable after<br />

the program uses its value. Therefore, the postincrementing form seems more natural. Preincrementing<br />

and postincrementing have the same effect in the increment expression, because<br />

the increment does not appear in a larger expression. The two semicolons in the for<br />

structure are required.<br />

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

Using commas instead of the two required semicolons in a for header is a syntax error. 5.4<br />

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

Placing a semicolon immediately <strong>to</strong> the right of the right parenthesis of a for header makes<br />

the body of that for structure an empty statement. This is normally a logic error. 5.5<br />

The initialization, loop-continuation condition and increment portions of a for structure<br />

can contain arithmetic expressions. For example, assume that x =2 and y =10. If x<br />

and y are not modified in the body of the loop, the statement<br />

for ( int j = x; j

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

Saved successfully!

Ooh no, something went wrong!