Essentials of Counter-Controlled Repetition

This section uses the while repetition statement introduced in Chapter 4 to formalize the elements required to perform counter-controlled repetition. Counter-controlled repetition requires

  1. a control variable (or loop counter)
  2. the initial value of the control variable
  3. the increment (or decrement) by which the control variable is modified each time through the loop (also known as each iteration of the loop)
  4. the loop-continuation condition that determines whether looping should continue.

To see these elements of counter-controlled repetition, consider the application of Fig. 5.1, which uses a loop to display the numbers from 1 through 10. Note that Fig. 5.1 contains only one method, main, which does all of the class's work. For most applications in Chapters 34, we have encouraged the use of two separate filesone that declares a reusable class (e.g., Account) and one that instantiates one or more objects of that class (e.g., AccountTest) and demonstrates its (their) functionality. Occasionally, however, it is more appropriate simply to create one class whose main method concisely illustrates a basic concept. Throughout this chapter, we use several one-class examples like Fig. 5.1 to demonstrate the mechanics of Java's control statements.

Figure 5.1. Counter-controlled repetition with the while repetition statement.

 1 // Fig. 5.1: WhileCounter.java
 2 // Counter-controlled repetition with the while repetition statement.
 3
 4 public class WhileCounter
 5 {
 6 public static void main( String args[] )
 7 {
 8 int counter = 1; // declare and initialize control variable
 9
10 while ( counter <= 10 ) // loop-continuation condition
11 {
12 System.out.printf( "%d ", counter );
13 ++counter; // increment control variable by 1
14 } // end while
15
16 System.out.println(); // output a newline
17 } // end main
18 } // end class WhileCounter
 
1 2 3 4 5 6 7 8 9 10
 

In main of Fig. 5.1 (lines 617), the elements of counter-controlled repetition are defined in lines 8, 10 and 13. Line 8 declares the control variable (counter) as an int, reserves space for it in memory and sets its initial value to 1. Variable counter could also have been declared and initialized with the following local-variable declaration and assignment statements:

 int counter; // declare counter
 counter = 1 ; // initialize counter to 1

Line 12 in the while statement displays control variable counter's value during each iteration of the loop. Line 13 increments the control variable by 1 for each iteration of the loop. The loop-continuation condition in the while (line 10) tests whether the value of the control variable is less than or equal to 10 (the final value for which the condition is true). Note that the program performs the body of this while even when the control variable is 10. The loop terminates when the control variable exceeds 10 (i.e., counter becomes 11).

Common Programming Error 5.1

Because floating-point values may be approximate, controlling loops with floating-point variables may result in imprecise counter values and inaccurate termination tests.

Error-Prevention Tip 5.1

Control counting loops with integers.

Good Programming Practice 5.1

Place blank lines above and below repetition and selection control statements, and indent the statement bodies to enhance readability.

The program in Fig. 5.1 can be made more concise by initializing counter to 0 in line 8 and preincrementing counter in the while condition as follows:

 while ( ++counter <= 10 ) // loop-continuation condition
 System.out.printf( "%d ", counter );

This code saves a statement (and eliminates the need for braces around the loop's body), because the while condition performs the increment before testing the condition. (Recall from Section 4.12 that the precedence of ++ is higher than that of <=.) Coding in such a condensed fashion takes practice and might make code more difficult to read, debug, modify and maintain, and typically should be avoided.

Software Engineering Observation 5.1

"Keep it simple" remains good advice for most of the code you will write.


Introduction to Computers, the Internet and the World Wide Web

Introduction to Java Applications

Introduction to Classes and Objects

Control Statements: Part I

Control Statements: Part 2

Methods: A Deeper Look

Arrays

Classes and Objects: A Deeper Look

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism

GUI Components: Part 1

Graphics and Java 2D™

Exception Handling

Files and Streams

Recursion

Searching and Sorting

Data Structures

Generics

Collections

Introduction to Java Applets

Multimedia: Applets and Applications

GUI Components: Part 2

Multithreading

Networking

Accessing Databases with JDBC

Servlets

JavaServer Pages (JSP)

Formatted Output

Strings, Characters and Regular Expressions

Appendix A. Operator Precedence Chart

Appendix B. ASCII Character Set

Appendix C. Keywords and Reserved Words

Appendix D. Primitive Types

Appendix E. (On CD) Number Systems

Appendix F. (On CD) Unicode®

Appendix G. Using the Java API Documentation

Appendix H. (On CD) Creating Documentation with javadoc

Appendix I. (On CD) Bit Manipulation

Appendix J. (On CD) ATM Case Study Code

Appendix K. (On CD) Labeled break and continue Statements

Appendix L. (On CD) UML 2: Additional Diagram Types

Appendix M. (On CD) Design Patterns

Appendix N. Using the Debugger

Inside Back Cover



Java(c) How to Program
Java How to Program (6th Edition) (How to Program (Deitel))
ISBN: 0131483986
EAN: 2147483647
Year: 2003
Pages: 615

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net