Peter's Gekko

Sponsors

The Lounge

News

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
do while, do not repeat

In C# you can express a lot using with a minimal set of statements. One of the active goals of the C# team is to keep the language as simple and small as possible. Another (small) example of this is the flexbility of the while statement. Take this snippet of code

string dirName = @"C:\USR";
string fileName = "*.cs"
;
bool fileMissing = true;

while(fileMissing)
{
   fileName = myFiddleFuction(fileName);
   fileMissing = Directory.GetFiles(dirName, fileName).Length == 0;
}

This can also be expressed as :

string dirName = @"C:\USR";
string fileName = "*.cs";

do
{  
   fileName = myFiddleFuction(fileName);
}
while(Directory.GetFiles(dirName, fileName).Length == 0);

The control variable fileMissing, which had to make sure the loop was executed at least once, is gone. If you ever worked with a language like Pascal (Delphi) you'll propably recognize the repeat statement.

Peter


Posted 02-19-2004 10:04 AM by pvanooijen

[Advertisement]

Comments

Frans Bouma wrote re: do while, do not repeat
on 02-19-2004 5:51 AM
do while is semantically different from while. do while is an 1-n loop, while a 0-n loop. You can use both in 1-n scenario, but you can't use both in a 0-n scenario.

That's why I think it's important to understand the difference between the two and to keep them separated.

I prefer for() {} though, as it is much more powerful than while or do while, as you can specify the loop specific declarations in the for header.

You can often tell if hte developer used a lot of Wirth languages (in case he will use while a lot) or C (for loops). :)
Peter van Ooijen wrote re: do while, do not repeat
on 02-19-2004 6:21 AM
Wirth it is !

It's hard to compare for in a language like pascal with for in C#. In Pascal the number of iterations is determined at the start of the loop. To break out of the loop you need something like a break;
In C# an expression is evaluated on every iteration, you can even code something like
for (;Directory.GetFiles(dirName, fileName).Length == 0;)
{
fileName = myFiddleFuction(fileName);
}
Which is impossible in Pascal.
Peter's Gekko wrote FORe a WHILE
on 02-19-2004 7:55 AM
FORe a WHILE
Kirk Graves wrote re: do while, do not repeat
on 02-19-2004 9:28 AM
Nice compact code sample, but it is also much less readable then the first sample, and there would be no appreciable performance advantage.

Having worked on projects with multiple developers on many occasions, and where each of those developers was at a different level of expertise, I find readable code to be the second most important issue when deciding how to implement a particular algorythm. (The first is of course performance).
Peter van Ooijen wrote re: do while, do not repeat
on 02-19-2004 1:48 PM
Isn't that excactly what Frans is pointing at ? Using do while makes it explicit that the code loop in the loop always has to run. Which could be crucial. Maybe a login would have made a better example.
James Curran wrote re: do while, do not repeat
on 02-19-2004 2:48 PM
The second could be written as:

do
{
fileName = myFiddleFuction(fileName);
} while(Directory.GetFiles(dirName, fileName).Length == 0);


while more readable, this version also more clearly shows what's different between the "while" and "do..while" versions
Peter van Ooijen wrote re: do while, do not repeat
on 02-20-2004 12:40 AM
Thanks James, post is updated.

Add a Comment

(required)  
(optional)
(required)  
Remember Me?