Using dowhile to Loop an Indeterminate Number of Times

   

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  );  
graphics/bookpencil.gif

The following is used to execute multiple statements:

 do {    [Statements] }  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.

graphics/15fig02.jpg


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.

graphics/bookpencil.gif

I wanted to show you how both types of loops could be used to accomplish the same task. In this example, however, the for loop is clearly the best approach because you know the starting values and the number of times to loop when the loop starts.

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 .


   
Top


Sams Teach Yourself C# in 24 Hours
Sams Teach Yourself Visual Basic 2010 in 24 Hours Complete Starter Kit (Sams Teach Yourself -- Hours)
ISBN: 0672331136
EAN: 2147483647
Year: 2002
Pages: 253
Authors: James Foxall

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