Yeah, wasn't paying attention to the body when I did that. (derp) The var should have an initial value, but I just figure if you're gonna use a for loop that breaks when a var is at a specific value then why not use the for-loop syntax?
Here's an upgraded version:
[source lang="cpp"]void operator,() {
for(int result = initial_val; result != x; ) {
//do stuff
result = whatever;
}
}[/source]
Anyway, I'll use 'while(true)' sometimes, although MSVC has an annoying warning about it. Suppose it's a matter of preference.
C Do/While Loop problem
Anyway, I'll use 'while(true)' sometimes, although MSVC has an annoying warning about it. Suppose it's a matter of preference.
What does the warning say? If it's because of "true", you can just use 1 (or any value different from zero, really). Or does it literally babysit you saying "this is an infinite loop"?
[quote name='Khatharr' timestamp='1354607178' post='5006998']
Anyway, I'll use 'while(true)' sometimes, although MSVC has an annoying warning about it. Suppose it's a matter of preference.
What does the warning say? If it's because of "true", you can just use 1 (or any value different from zero, really). Or does it literally babysit you saying "this is an infinite loop"?
[/quote]
"while (1)" throws the warning as well - conditional expression is constant. Personally I prefer to have the warning and just use "for (;;)" instead - the warning is far more useful than being able to do "while (1)".
I agree that the warning could potentially be useful, but I've probably triggered it a million times and it was always an intentional unconditional loop.
Pity there's no explicit unconditional loop.
"for(;;)" looks like the arachnid version of a revolutionary's wall art. Or maybe it's a walrus?
Do we have an ASCII expert in here?
Pity there's no explicit unconditional loop.
"for(;;)" looks like the arachnid version of a revolutionary's wall art. Or maybe it's a walrus?
Do we have an ASCII expert in here?
Do we have an ASCII expert in here?
What do you need an ASCII expert for (not claiming to be one)?
"for(;;)" looks like the arachnid version of a revolutionary's wall art. Or maybe it's a walrus?[/quote]
Well, I agree that for(;;) looks horrible and is much less readable at a glance than while(true), but if you can just remember that it means "loop indefinitely" then I guess it's fine.
I agree that the warning could potentially be useful, but I've probably triggered it a million times and it was always an intentional unconditional loop.
Have a read: http://stackoverflow.com/questions/3490823/why-msvc-generates-warning-c4127-whan-constant-is-used-in-while-c - it outlines the general usefulness of the warning. My general philosophy on this is that if it saves your ass even just once then it's probably worth it.
The one case where I do agree that it's a pain is "do { ... } while (1)".
Pity there's no explicit unconditional loop.
According to the link above "for (;;)" is actually what is explicitly defined to be an infinite/unconditional loop, but I haven't cross-checked with the standard so take it with the appropriately sized grain of salt.
"for(;;)" looks like the arachnid version of a revolutionary's wall art. Or maybe it's a walrus?
Definitely a walrus.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement