While Loop

Cags

Contributor
Joined
Feb 19, 2004
Messages
695
Location
Melton Mowbray, England
I was just wondering if anything could be considered wrong with using the following...
C#:
while(MyFunction()) {}
Basically the functionality of the while is moved to a seperate function, am I likely to encounter any problem using this syntax, or would it be better todo...
C#:
bool isSuccess = true;
while(isSuccess)
{
       isSuccess = MyFunction();
}
As far as I can tell both these sections of code will perform the same, I just thought I'd put the question out incase theres something I'm missing. The last thing I wan't is for my application to start throwing random errors somewhere down the line.
 
My opinions (because it's really just going to come down to style preference):

The first chunk of code is very C, very macho coder. I like it. My only caveats are that MyFunction is named something useful in real life, and that there aren't any major side effects by calling the MyFunction (in either case, really).

I personally think the first code chunk is more readable because everything is contained in the loop invariant. You know right at the top how the loop is affected rather than having to search through the loop to find what modifies isSuccess. I like that you don't have to declare a variable that you're just going to throw away. There isn't a speed concern here because you'll be running the method each time through the loop in either case.

The only minor thing is that the two chunks as you wrote them aren't quite equivalent (this is just me being a little nit picky) :)
C#:
bool isSuccess = MyFunction();  //the first chunk did the check before going into the loop so this one should too.
while(isSuccess)
{
       isSuccess = MyFunction();
}
Others may make the argument for chunk two, though again, I think it comes down to style -- I don't really see a technical/performance concern.
 
I see your point they weren't "Technically" the same :p. It just looks really strange because no extra code is inside the while loop, hence the entire code is proccessed as part of the loop invariant, leading to a single line while statement.
 
There is absolutely nothing with that. It can be very useful sometimes. For example, just say that instead of using a foreach loop, you decided to be crazy and use the IEnumerator yourself. Instead of writing code like this:
C#:
string[] stuff = new string[] {"A", "B", "C"};

foreach(string s in stuff) {
	MessageBox.Show(s);
}
You could write code like this:
C#:
string[] stuff = new string[] {"A", "B", "C"};

IEnumerator stuffEnumerator= ((IEnumerable)stuff).GetEnumerator();

while(stuffEnumerator.MoveNext()) {
	MessageBox.Show((string)(stuffEnumerator.Current));
}
Granted the code is not pretty and not worth the extra effort, but it demonstrates exactly what the While(Function()) type of loop is good for: when an object has a function that performs an action and returns a value that indicates that it should continue to be looped on/enumerated. In fact, this is exactly the pattern that compilers use for enumerable objects other than arrays. This is decompiled code from a foreach on a List<> object.
C#:
List<int> list1 = new List<int>();
List<int>.Enumerator enumerator2 = list1.GetEnumerator();
try{
    while (enumerator2.MoveNext())
    {
        int num1 = enumerator2.get_Current();
    }
}

[Edit]I guess mskeel writes replies alot faster than I do. And you too, cags.[/edit]
 
Back
Top