No, I meant break.
I seem to often run into where this would be a handy feature where the condition requires some preparation, such as in snippets like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Custom classes are used, but the theory remains the same.
String key = "RawData.";
ReferenceString value;
size_t offset;
unsignedint i = 1;
while (true)
{
offset = key.aformat("%u", i); // append a number to the INI key
value.set(this->readConfigValue(key)); // Fetch the key's value
if (value.isEmpty()) break; // If no value, then this is the end of the list
key.truncate(offset); // remove the number from the key.
this->send(value); // send the data
i++;
}
Which is mostly an annoyance, since these are the only warnings I still get when compiling on MSVC with /W4. I could do what firedraco suggested, though I'm just curious why this (a code body after the while in a do-while loop) isn't a thing.
A agree that it's fairly common to want to break out of a loop in the middle. Some books and people will say that you shouldn't do this but the code that avoids it is usually uglier than the code that does it.
I don't know why languages don't have something like the syntax you suggest, except that I've noticed that most languages like to do things one way and the designers generally say "since it can be done in a do while (true) loop, there's no need for it." Of course the same can be said for a for loop, which is the same as a while loop.
Just a little readability trick I use occasionally:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
String key = "RawData.";
ReferenceString value;
size_t offset;
unsignedint i = 1;
auto loop_condition = [&]
{
offset = key.aformat("%u", i);
value.set(readConfigValue(key));
return value.isEmpty();
}
while (loop_condition())
{
key.truncate(offset); // remove the number from the key.
send(value); // send the data
i++;
}
(which can look roughly like the do-while construct in the OP.)
loop_condition = [&]
Can you explain what this is or provide a reference. I couldn't find it. It looks like you're defining a function inside another function.