The amazing for...while() construct
-
You know that common C dilemma people have? How to prevent a "for" loop from rebelliously over-running the boundaries you set it? Well I picked up this cunning solution from my co-workers code:
for (int i=0; i<nMaxValue; i++)
{
do_stuff();
}
while(false);
That last statement makes all the difference, really.
-
@cheesemonkey said:
You know that common C dilemma people have? How to prevent a "for" loop from rebelliously over-running the boundaries you set it? Well I picked up this cunning solution from my co-workers code:
for (int i=0; i<nMaxValue; i++)
{
do_stuff();
}
while(false);
That last statement makes all the difference, really.
Cool - I wouldn't even have thought that this might compile...?
-
@tdittmar said:
Why ever not? There's nothing (syntactically) wrong with it. Would some more redundancy help you? Replace the last semicolon with an empty block:@cheesemonkey said:
You know that common C dilemma people have? How to prevent a "for" loop from rebelliously over-running the boundaries you set it? Well I picked up this cunning solution from my co-workers code:
for (int i=0; i<nMaxValue; i++)
{
do_stuff();
}
while(false);
That last statement makes all the difference, really.
Cool - I wouldn't even have thought that this might compile...?
for (int i=0; i<nMaxValue; i++)
{
do_stuff();
}
while(false)
{
/* do nothing. */
}
-
@PJH said:
@tdittmar said:
Why ever not? There's nothing (syntactically) wrong with it. Would some more redundancy help you? Replace the last semicolon with an empty block:@cheesemonkey said:
You know that common C dilemma people have? How to prevent a "for" loop from rebelliously over-running the boundaries you set it? Well I picked up this cunning solution from my co-workers code:
for (int i=0; i<nMaxValue; i++)
{
do_stuff();
}
while(false);
That last statement makes all the difference, really.Cool - I wouldn't even have thought that this might compile...?
for (int i=0; i<nMaxValue; i++)
{
do_stuff();
}
while(false)
{
/* do nothing. */
}Oh right, you're correct. I somehow interpreted the while to belong to the for loop. Something like "loop from 0 to nMaxValue while false". You know, like the "do { ... } while...;" loop only with the "do" replaced by "for (...)".
-
That's what's wrong with the code! The while is outside the for loop, and there's nothing inside the while!
for (int i=0; i<nMaxValue; i++)
{
do_stuff();
while (false)
{
break;
}
}
See? That works much better! </sarcasm>
-
@Erick said:
That's what's wrong with the code! The while is outside the for loop, and there's nothing inside the while!
for (int i=0; i<nMaxValue; i++)
{
do_stuff();
while (false)
{
break;
}
}
See? That works much better!
Genius! Thanks for this I'll let my co-worker know.
-
@Erick said:
That's what's wrong with the code! The while is outside the for loop, and there's nothing inside the while!
for (int i=0; i<nMaxValue; i++)
{
do_stuff();
while (false)
{
break;
}
}
See? That works much better!
LOL Good when being paid per Lines Of Code
-
@tdittmar said:
@Erick said:
That's what's wrong with the code! The while is outside the for loop, and there's nothing inside the while!
for (int i=0; i<nMaxValue; i++)
{
do_stuff();
while (false)
{
break;
}
}
See? That works much better!
LOL Good when being paid per Lines Of Code
Nobody who is paid per line would ever use a for loop.
/* Enterprise looping strategy */ bool done; int i; i = 0; done = false; while (!done) { assert(i < nMaxValue); do_stuff(); int next; next = i + 1; assert(next <= nMaxValue) if (next == nMaxValue) { done = true; break; } else { assert(next < nMaxValue); i = next; continue; } }
-
@asuffield said:
@tdittmar said:
@Erick said:
That's what's wrong with the code! The while is outside the for loop, and there's nothing inside the while!
for (int i=0; i<nMaxValue; i++)
{
do_stuff();
while (false)
{
break;
}
}
See? That works much better!
LOL Good when being paid per Lines Of Code
Nobody who is paid per line would ever use a for loop.
/* Enterprise looping strategy */ bool done; int i; i = 0; done = false; while (!done) { assert(i < nMaxValue); do_stuff(); int next; next = i + 1; assert(next <= nMaxValue) if (next == nMaxValue) { done = true; break; } else { assert(next < nMaxValue); i = next; continue; } }
It lacks Looper and LoopFactory classes.
-
I just wrote this as a joke, but now I feel all dirty inside.
public static class LoopFactory
{
public static LoopInstance GetLoopInstance(int start, int end, LoopInstance.CompareType comparer)
{
///More Enterprisy code here
return new LoopInstance(start);
}
}
public class LoopInstance
{
public enum CompareType
{
Greater,
Lesser,
Equals,
FILE_NOT_FOUND
}
private int _count;
public int Count
{
get
{
return _count;
}
set
{
_count = value;
}
}
public LoopInstance()
{
Count = 0;
}
public LoopInstance(int start)
{
Count = start;
}
public Boolean Compare(int compareTo, CompareType comparer)
{
if (comparer = CompareType.Greater)
{
if (compareTo > Count)
{
return true;
}
else
{
return false;
}
}
else if (comparer = CompareType.Lesser)
{
if (compareTo < Count)
{
return true;
}
else
{
return false;
}
}
else if (comparer = CompareType.Equals)
{
if (compareTo == Count)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
}
-
You should be programming to an interface, not an implementation. Make LoopInstance an interface, and create a class called LoopInstanceImpl to implement it. You'll get an extra 20 lines of code out of that.
-
Please, just stop. You're making me weep for the future.
-
@asuffield said:
The fact that, that code, appeared all on one lne in the email notification sorta detracted from it's purpose.@tdittmar said:
*LOL* Good when being paid per Lines Of Code
Nobody who is paid per line would ever use a for loop.
/* Enterprise looping strategy */ bool done; int i; i = 0; done = false; while (!done) { assert(i < nMaxValue); do_stuff();
int next;
next = i + 1;
assert(next <= nMaxValue)
if (next == nMaxValue) {
done = true;
break;
}
else {
assert(next < nMaxValue);
i = next;
continue;
}
}
-
@PJH said:
The fact that, that code, appeared all on one lne in the email notification sorta detracted from it's purpose.
It seems that the forum software is still the real WTF. A pre block explicitly means "DO NOT REFORMAT THIS YOU BLITHERING MORON", and they managed to reformat it.
-
ausffield, if you're getting paid for LOC, you probably should put your opening braces on the next line.
-
I'm assuming that it's a not-completely-retarded LOC counter, and so is counting actual statements rather than newline characters. Otherwise there are going to be a lot of blank lines and comments.
-
@Jonathan Holland said:
Nice! This reminds me of a game we used to play in college: who can be more stupid? I always won. Wait, did that mean that I lost?wrote this as a joke, but now I feel all dirty inside.
public static class LoopFactory
{
public static LoopInstance GetLoopInstance(int start, int end, LoopInstance.CompareType comparer)
{
///More Enterprisy code here
return new LoopInstance(start);
}
}
public class LoopInstance
{
public enum CompareType
{
Greater,
Lesser,
Equals,
FILE_NOT_FOUND
}
private int _count;
public int Count
{
get
{
return _count;
}
set
{
_count = value;
}
}
public LoopInstance()
{
Count = 0;
}
public LoopInstance(int start)
{
Count = start;
}
public Boolean Compare(int compareTo, CompareType comparer)
{
if (comparer = CompareType.Greater)
{
if (compareTo > Count)
{
return true;
}
else
{
return false;
}
}
else if (comparer = CompareType.Lesser)
{
if (compareTo < Count)
{
return true;
}
else
{
return false;
}
}
else if (comparer = CompareType.Equals)
{
if (compareTo == Count)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
}
-
@asuffield said:
It seems that the forum software is still the real WTF. A pre block explicitly means "DO NOT REFORMAT THIS YOU BLITHERING MORON", and they managed to reformat it.
It's just that plain-text notifications are completely broken (they're missing all linefeeds). Switch to HTML, and it'll kinda work (though they randomly refer to images on server through their relative path).
-
@Jonathan Holland said:
if (comparer = CompareType.Greater)
else if (comparer = CompareType.Lesser)
else if (comparer = CompareType.Equals)
if (compareTo == Count)
...one of these is not like the others (though it's the only one that will compile)
-
@Jonathan Holland said:
wrote this as a joke, but now I feel all dirty inside.
...
The code appears to only manage loop state, nothing more. Can't you use a delegate to have the class actually run the loop code for you?