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
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
|
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).
|
|
|
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.
|
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