Essentials of Counter-Controlled Repetition

This section uses the while repetition statement introduced in Chapter 5 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. 6.1, which uses a loop to display the numbers from 1 through 10. Note that Fig. 6.1 contains only one method, Main, which does all of the class's work. For most applications in Chapters 4 and 5, 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 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. 6.1 to demonstrate the mechanics of various C# control statements.

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

 1 // Fig. 6.1: WhileCounter.cs
 2 // Counter-controlled repetition with the while repetition statement.
 3 using System;
 4
 5 public class WhileCounter
 6 {
 7 public static void Main( string[] args )
 8 {
 9 int counter = 1; // declare and initialize control variable
10
11 while ( counter <= 10 ) // loop-continuation condition
12 {
13 Console.Write( "{0} ", counter );
14 counter++; // increment control variable
15 } // end while
16
17 Console.WriteLine(); // output a newline
18 } // end Main
19 } // end class WhileCounter
 
1 2 3 4 5 6 7 8 9 10

In method Main of Fig. 6.1 (lines 718), the elements of counter-controlled repetition are defined in lines 9, 11 and 14. Line 9 declares the control variable (counter) as an int, reserves space for it in memory and sets its initial value to 1.

Line 13 in the while statement displays control variable counter's value during each iteration of the loop. Line 14 increments the control variable by 1 for each iteration of the loop. The loop-continuation condition in the while (line 11) 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 application 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 6 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 6 1

Control counting loops with integers.

Good Programming Practice 6 1

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

The application in Fig. 6.1 can be made more concise by initializing counter to 0 in line 9 and incrementing counter in the while condition with the prefix increment operator as follows:

while ( ++counter <= 10 ) // loop-continuation condition
 Console.Write( "{0} ", 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 5.12 that the precedence of ++ is higher than that of <=.) Coding in such a condensed fashion might make code more difficult to read, debug, modify and maintain.

Software Engineering Observation 6 1

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


Preface

Index

    Introduction to Computers, the Internet and Visual C#

    Introduction to the Visual C# 2005 Express Edition IDE

    Introduction to C# Applications

    Introduction to Classes and Objects

    Control Statements: Part 1

    Control Statements: Part 2

    Methods: A Deeper Look

    Arrays

    Classes and Objects: A Deeper Look

    Object-Oriented Programming: Inheritance

    Polymorphism, Interfaces & Operator Overloading

    Exception Handling

    Graphical User Interface Concepts: Part 1

    Graphical User Interface Concepts: Part 2

    Multithreading

    Strings, Characters and Regular Expressions

    Graphics and Multimedia

    Files and Streams

    Extensible Markup Language (XML)

    Database, SQL and ADO.NET

    ASP.NET 2.0, Web Forms and Web Controls

    Web Services

    Networking: Streams-Based Sockets and Datagrams

    Searching and Sorting

    Data Structures

    Generics

    Collections

    Appendix A. Operator Precedence Chart

    Appendix B. Number Systems

    Appendix C. Using the Visual Studio 2005 Debugger

    Appendix D. ASCII Character Set

    Appendix E. Unicode®

    Appendix F. Introduction to XHTML: Part 1

    Appendix G. Introduction to XHTML: Part 2

    Appendix H. HTML/XHTML Special Characters

    Appendix I. HTML/XHTML Colors

    Appendix J. ATM Case Study Code

    Appendix K. UML 2: Additional Diagram Types

    Appendix L. Simple Types

    Index



    Visual C# How to Program
    Visual C# 2005 How to Program (2nd Edition)
    ISBN: 0131525239
    EAN: 2147483647
    Year: 2004
    Pages: 600

    Similar book on Amazon

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