Sams Teach Yourself C# in 24 Hours
- Table of Contents
- Copyright
- About the Authors
- Acknowledgments
- Tell Us What You Think!
- Introduction
- Audience and Organization
- Conventions Used in This Book
- Onward and Upward!
- Part I. The Visual Studio Environment
- Hour 1. A C# Programming Tour
- Hour 2. Navigating C#
- Hour 3. Understanding Objects and Collections
- Hour 4. Understanding Events
- Part II. Building a User Interface
- Hour 5. Building FormsPart I
- Hour 6. Building FormsPart II
- Hour 7. Working with the Traditional Controls
- Hour 8. Advanced Controls
- Hour 9. Adding Menus and Toolbars to Forms
- Hour 10. Drawing and Printing
- Part III. Making Things HappenProgramming!
- Hour 11. Creating and Calling Methods
- Hour 12. Using Constants, Data Types, Variables, and Arrays
- Hour 13. Performing Arithmetic, String Manipulation, and Date/Time Adjustments
- Hour 14. Making Decisions in C# Code
- Hour 15. Looping for Efficiency
- Looping a Specific Number of Times Using for Statements
- Using dowhile to Loop an Indeterminate Number of Times
- Summary
- Q&A
- Workshop
- Hour 16. Debugging Your Code
- Hour 17. Designing Objects Using Classes
- Hour 18. Interacting with Users
- Part IV. Working with Data
- Hour 19. Performing File Operations
- Hour 20. Controlling Other Applications Using Automation
- Hour 21. Working with a Database
- Part V. Deploying Solutions and Beyond
- Hour 22. Deploying a Solution
- Hour 23. Introduction to Web Development
- Hour 24. The 10,000-Foot View
- Appendix A. Answers to Quizzes/Exercises
Using do…while to Loop an Indeterminate Number of Times
In some situations, you won't know the exact number of times a loop needs to be performed—not even when the loop begins. When you need to create such a loop, using the do…while loop is the best solution.
The do…while comes in a number of flavors. Its most basic form has the following syntax:
do statement while (expression);
Ending a do…while
A do…while without some sort of exit mechanism or defined condition is an endless loop. In its most basic form, nothing is present to tell the loop when to stop looping. At times you may need an endless loop (game programming is an example), but more often, you'll need to exit the loop when a certain condition is met. Like the for loop, you can use the break statement to exit a do…while loop at any time. For example, you could expand the do…while we're discussing to include a break statement like the following:
do
{
[Statements]
if (expression)
break;
} while (
x==x
);
In this code, the loop would execute until expression evaluates to true. Generally, the expression is based on a variable that's modified somewhere within the loop. Obviously, if the expression never changes, the loop never ends.
The second flavor of the while loop is the while…do loop. The following is a simple while…do loop:
while ( expression ) statement
As long as expression evaluates to true, the loop continues to occur. If expression evaluates to false when the loop first starts, the code between the statement(s) doesn't execute—not even once.
The difference between the do…while and the while…do loops is that the code statements within the do…while loop always execute at least once; expression isn't evaluated until the loop has completed its first cycle. Therefore, such a loop executes at least once, regardless of the value of expression. The while loop evaluates the expression first; therefore, the statements associated with it may not execute at all.
Creating a do…while Example
You're now going to create an example using a do…while loop that updates a label to once again simulate a progress meter. This loop performs the same purpose as the for loop you created in the previous example, but its structure is quite different.
If your for example is currently running, choose Stop Debugging from the Debug menu. Add another button to the formand set the new button's properties as follows:
| Property | Value |
| Name | btnDoLoop |
| Location | 88,160 |
| Size | 125,23 |
| Text | Run a Do Loop |
Your form should now look like the one shown in Figure 15.2.
Figure 15.2 You'll use the same form for both loop examples.
Double-click the new button to access its Click event and then enter the following code:
int intLabelWidth=1;
while (intLabelWidth != 100)
{
lblMeter.Width = intLabelWidth;
lblMeter.Refresh();
intLabelWidth++; // Increment by one.
for (int intPauseCounter=1; intPauseCounter<=600000; intPauseCounter++);
}
Again, this code is more easily understood when broken down, as shown in the following list:
- The first line creates the intLabelWidth variables and sets it to 1.
- A while statement is used to start a loop. This loop will execute until intLabelWidth has the value 100. It can also be read as "the statements will execute while intLabelWidth does not equal 100."
- The Label's Width property is set to the value of intLabelWidth.
- The Label is forced to refresh its appearance.
- The intLabelWidth variable is incremented by 1.
- A for loop is used to slow down the procedure, just as it was in the previous example. Again, this is just to illustrate nested loops. Calling System.Threading.Thread.Sleep() would be a better way to create a delay.
- Click Save All on the toolbar to save the project, and then press F5 to run it. Click the Run a Do Loop button to see the progress meter set itself to 1 pixel and then update itself by 1 until it reaches 100 pixels.
You've now seen how to perform the same function with two entirely different coding techniques. This is fairly common when creating programs; usually, multiple ways exist to approach a solution to a given problem. Simply being aware of your options makes writing code that much easier. When you hear of people optimizing their code, they're usually looking for a different, faster approach to a problem they've already solved.
Summary | Next Section

Account Sign In
View your cart