Do you think do-while statements are ugly?
-
If you are looking for a more readable form, then why not use a
for-loop
? They are not more elegant, but easier to read, especially to an inexperienced developer. Other than that, your code snippets 1 and 3 are fine, while 2 is ugly (bad code styling). Another variation, that you didn't consider, would be:int num = 0;
do
{
Console.WriteLine("Number: " + num.ToString());
}
while(num++ < 5);This also got me thinking, C++ and C# are missing a simpler form of a loop that doesn't need a variable. However, it is too easy to implement yourself. Example:
// repeat n times, or until break is encountered:
repeat(n)
{
}Would such operator make life easier? ;)
Let's agree to disagree! Boris the animal Just Boris.
Also, how would you propose implementing it yourself? I don't see a way to add it with C# in a way that is seamless and doesn't look incredibly hacky with lambdas and such.
-
This isn't for cases that can be covered with the for-loop, I use that (and foreach) whenever possible - my example is contrived and simplified. I didn't intend on an example that is better suited for a for loop. Usually my condition is something like stream.HasMoreData() or something of the sort. I would actually prefer a simple loop like that, plus another variation that doesn't take a number at all, just keeps going until break is encountered, i.e:
loop
{
// codeif (condition) break;
}
Then you have a simple construct that you can use for while loops, do-while loops, or anything in-between if you so desire. Just move the position of the condition to where you want to break out.
That's what I suggested just above, with operand
repeat
, but you also want it without the parameter, which is legitimate :) Hey, somebody needs to quickly update C++ and C# compilers to support the new syntax! :)// repeat n times, or until break is encountered:
repeat(n)
{
}// repeat indefinitely, or until break is encountered:
repeat
{
}However, the second version wouldn't be of the same value, because it is easier to replicate with already existing syntax of:
while(true)
{
}but it surely is nicer (more elegant) nonetheless :)
Let's agree to disagree! Boris the animal Just Boris.
-
I find myself using regular while loops instead, even if the check will be performed one extra time, i.e:
int num = 0;
while (num < 5)
{
Console.WriteLine("Number: " + num++);
}or sometimes I'll do this, if I really want to avoid an expensive conditional check the first run through:
int num = 0;
while (true)
{
Console.WriteLine("Number: " + num++);if (num >= 5) { break; }
}
instead of the do-while version:
int num = 0;
do
{
Console.WriteLine("Number: " + num++);
} while (num < 5);Obviously this is an over-simplified example, but you get the idea. I find the alternatives more readable than the do-while loop, which just feels like an awkward construct. That while tacked on the end is, well, awkward. Do you guys use do-while everywhere you know the loop will run at least once, or do you tend to cheat like me because you think do-while's are icky? EDIT (for those just joining in): Joan Murt posted the best solution I've seen so far below: http://www.codeproject.com/Lounge.aspx?msg=4295326#xx4295326xx[^]
I agree, but I've encountered very few situations (perhaps none) where one would be useful anyway.
-
I find myself using regular while loops instead, even if the check will be performed one extra time, i.e:
int num = 0;
while (num < 5)
{
Console.WriteLine("Number: " + num++);
}or sometimes I'll do this, if I really want to avoid an expensive conditional check the first run through:
int num = 0;
while (true)
{
Console.WriteLine("Number: " + num++);if (num >= 5) { break; }
}
instead of the do-while version:
int num = 0;
do
{
Console.WriteLine("Number: " + num++);
} while (num < 5);Obviously this is an over-simplified example, but you get the idea. I find the alternatives more readable than the do-while loop, which just feels like an awkward construct. That while tacked on the end is, well, awkward. Do you guys use do-while everywhere you know the loop will run at least once, or do you tend to cheat like me because you think do-while's are icky? EDIT (for those just joining in): Joan Murt posted the best solution I've seen so far below: http://www.codeproject.com/Lounge.aspx?msg=4295326#xx4295326xx[^]
Both alternative 1 & 3 are perfectly acceptable to me, depending on whether or not I want to check the condition in the beginning or the end of the loop - there can be good reason for one or the other approach. What I DON'T like is alternative 2 - Doing
while (true)
is to me a wrong use of the loop. True is not a condition and it's never gonna change, hence the risk of creating an eternal loop exists.Why can't I be applicable like John? - Me, April 2011
-----
Beidh ceol, caint agus craic againn - Seán Bán Breathnach
-----
Da mihi sis crustum Etruscum cum omnibus in eo!
-----
Just because a thing is new don’t mean that it’s better - Will Rogers, September 4, 1932 -
I find myself using regular while loops instead, even if the check will be performed one extra time, i.e:
int num = 0;
while (num < 5)
{
Console.WriteLine("Number: " + num++);
}or sometimes I'll do this, if I really want to avoid an expensive conditional check the first run through:
int num = 0;
while (true)
{
Console.WriteLine("Number: " + num++);if (num >= 5) { break; }
}
instead of the do-while version:
int num = 0;
do
{
Console.WriteLine("Number: " + num++);
} while (num < 5);Obviously this is an over-simplified example, but you get the idea. I find the alternatives more readable than the do-while loop, which just feels like an awkward construct. That while tacked on the end is, well, awkward. Do you guys use do-while everywhere you know the loop will run at least once, or do you tend to cheat like me because you think do-while's are icky? EDIT (for those just joining in): Joan Murt posted the best solution I've seen so far below: http://www.codeproject.com/Lounge.aspx?msg=4295326#xx4295326xx[^]
Mike Marynowski wrote:
That while tacked on the end is, well, awkward.
And sticking a break in an infinite loop isn't? Yuck! Talk about non-deterministic! X| A do/while is a perfectly nice construct, provided it's used where it's intended. I'd use it a lot more, if someone would invent a keyboard capable of providing a 50kV pulse to the finger of a user who presses the wrong button, despite clear instructions as to which button should be pressed. That's a command I'd want to ensure is executed at least once, then over and over again until the user stops pressing that button. :-D
Will Rogers never met me.
-
I find myself using regular while loops instead, even if the check will be performed one extra time, i.e:
int num = 0;
while (num < 5)
{
Console.WriteLine("Number: " + num++);
}or sometimes I'll do this, if I really want to avoid an expensive conditional check the first run through:
int num = 0;
while (true)
{
Console.WriteLine("Number: " + num++);if (num >= 5) { break; }
}
instead of the do-while version:
int num = 0;
do
{
Console.WriteLine("Number: " + num++);
} while (num < 5);Obviously this is an over-simplified example, but you get the idea. I find the alternatives more readable than the do-while loop, which just feels like an awkward construct. That while tacked on the end is, well, awkward. Do you guys use do-while everywhere you know the loop will run at least once, or do you tend to cheat like me because you think do-while's are icky? EDIT (for those just joining in): Joan Murt posted the best solution I've seen so far below: http://www.codeproject.com/Lounge.aspx?msg=4295326#xx4295326xx[^]
bool bContinue = true;
int num = 0;while (bContinue)
{
Console.WriteLine("Number: " + num++);
bContinue = (num < 5);
}The way I usually handle it if not using the 1 (which is the best IMHO) or the 3.
[www.tamautomation.com] Robots, CNC and PLC machines for grinding and polishing.
-
bool bContinue = true;
int num = 0;while (bContinue)
{
Console.WriteLine("Number: " + num++);
bContinue = (num < 5);
}The way I usually handle it if not using the 1 (which is the best IMHO) or the 3.
[www.tamautomation.com] Robots, CNC and PLC machines for grinding and polishing.
I like that one. I approve. Definitely going to steal that from you, if you don't mind :)
-
Mike Marynowski wrote:
That while tacked on the end is, well, awkward.
And sticking a break in an infinite loop isn't? Yuck! Talk about non-deterministic! X| A do/while is a perfectly nice construct, provided it's used where it's intended. I'd use it a lot more, if someone would invent a keyboard capable of providing a 50kV pulse to the finger of a user who presses the wrong button, despite clear instructions as to which button should be pressed. That's a command I'd want to ensure is executed at least once, then over and over again until the user stops pressing that button. :-D
Will Rogers never met me.
There are many situations where while(true) is the best way of doing something, particularly if there are many breakout locations in various nested conditional statements. Hence why, if I must avoid an expensive conditional check in the first run, I just adopted that pattern for the purpose. I'm used to seeing breaks at various points in while loops, sometimes "infinite" loops, so I don't really feel the "yuck"-ness of it. Joan Murt has the nicest solution I've seen so far which I'm definitely going to steal though, a couple posts down from yours :)
-
Both alternative 1 & 3 are perfectly acceptable to me, depending on whether or not I want to check the condition in the beginning or the end of the loop - there can be good reason for one or the other approach. What I DON'T like is alternative 2 - Doing
while (true)
is to me a wrong use of the loop. True is not a condition and it's never gonna change, hence the risk of creating an eternal loop exists.Why can't I be applicable like John? - Me, April 2011
-----
Beidh ceol, caint agus craic againn - Seán Bán Breathnach
-----
Da mihi sis crustum Etruscum cum omnibus in eo!
-----
Just because a thing is new don’t mean that it’s better - Will Rogers, September 4, 1932I like Joan Murt's way of doing it below, I'm stealing it. But in reply to your post, this is from my reply to another similar post below: There are many situations where while(true) is the best way of doing something, particularly if there are many breakout locations in various nested conditional statements. Hence why, if I must avoid an expensive conditional check in the first run, I just adopted that pattern for the purpose.
-
I find myself using regular while loops instead, even if the check will be performed one extra time, i.e:
int num = 0;
while (num < 5)
{
Console.WriteLine("Number: " + num++);
}or sometimes I'll do this, if I really want to avoid an expensive conditional check the first run through:
int num = 0;
while (true)
{
Console.WriteLine("Number: " + num++);if (num >= 5) { break; }
}
instead of the do-while version:
int num = 0;
do
{
Console.WriteLine("Number: " + num++);
} while (num < 5);Obviously this is an over-simplified example, but you get the idea. I find the alternatives more readable than the do-while loop, which just feels like an awkward construct. That while tacked on the end is, well, awkward. Do you guys use do-while everywhere you know the loop will run at least once, or do you tend to cheat like me because you think do-while's are icky? EDIT (for those just joining in): Joan Murt posted the best solution I've seen so far below: http://www.codeproject.com/Lounge.aspx?msg=4295326#xx4295326xx[^]
-
bool bContinue = true;
int num = 0;while (bContinue)
{
Console.WriteLine("Number: " + num++);
bContinue = (num < 5);
}The way I usually handle it if not using the 1 (which is the best IMHO) or the 3.
[www.tamautomation.com] Robots, CNC and PLC machines for grinding and polishing.
-
I like that one. I approve. Definitely going to steal that from you, if you don't mind :)
I do mind... this is under a patent that I own in all the countries in all the worlds of all the galaxies (known and unknown)... MUUUUWHAHAHAHAHAHAHAA! Please feel free to use it, I'm using this way when several conditions have to be evaluated even in different parts of the loop. ;)
[www.tamautomation.com] Robots, CNC and PLC machines for grinding and polishing.
-
This is only interesting if you have more than one condition, and avoids having a long test in the
while
brackets. In the very example you have given, I don't find it better than the solution 1.Yes, of course, all the options (except 2) are good and it all depends on preferences and/or the situation, typically I go for the one I proposed, but mostly in cases in which there are more than one condition to be handled. If there is only one condition, then I usually use the solution 1.
[www.tamautomation.com] Robots, CNC and PLC machines for grinding and polishing.
-
I find myself using regular while loops instead, even if the check will be performed one extra time, i.e:
int num = 0;
while (num < 5)
{
Console.WriteLine("Number: " + num++);
}or sometimes I'll do this, if I really want to avoid an expensive conditional check the first run through:
int num = 0;
while (true)
{
Console.WriteLine("Number: " + num++);if (num >= 5) { break; }
}
instead of the do-while version:
int num = 0;
do
{
Console.WriteLine("Number: " + num++);
} while (num < 5);Obviously this is an over-simplified example, but you get the idea. I find the alternatives more readable than the do-while loop, which just feels like an awkward construct. That while tacked on the end is, well, awkward. Do you guys use do-while everywhere you know the loop will run at least once, or do you tend to cheat like me because you think do-while's are icky? EDIT (for those just joining in): Joan Murt posted the best solution I've seen so far below: http://www.codeproject.com/Lounge.aspx?msg=4295326#xx4295326xx[^]
do/while
implies that the code must run at least once, whilewhile
by itself checks a condition before running the first time. Each has it's place. I use what's appropriate.".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997