C Do/While Loop problem

Started by
19 comments, last by alvaro 11 years, 4 months ago
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]

biggrin.png

Anyway, I'll use 'while(true)' sometimes, although MSVC has an annoying warning about it. Suppose it's a matter of preference.
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.
Advertisement

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"?

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

You lost me when you overloaded the comma operator.

[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)".

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

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?
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

Do we have an ASCII expert in here?

What do you need an ASCII expert for (not claiming to be one)?
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
Is there such a thing as an ASCII expert? There isn't all that much to know about it...
"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.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

Just #define ever (;;)

then you can do

for ever

;)
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

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.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

This topic is closed to new replies.

Advertisement