General Computer Science question

On multiple occasions, I've found myself writing code that requires a constant loop with the condition in the middle of the code segment.

1
2
3
4
5
6
while(true)
{
	// do stuff
	if (!condition) break; // Edit: Meant !condition instead of condition.
	// do more stuff
}


Are there any languages that have a do-while loop with this type of semantic?
1
2
3
4
5
6
7
8
do
{
	// do stuff
}
while (condition)
{
	// do more stuff
}


And if not, why?
Last edited on
You could write it like this instead (requires some duplication though):

1
2
3
4
5
6
// do stuff
while(condition)
{
	// do more stuff
	// do stuff
}
closed account (z05DSL3A)
It sounds like you want to use continue instead of break
1
2
3
4
5
6
7
8
9
10
11
12
13
    bool done = false;
    int i = 10;
    while (!done)
    {
        std::cout << i << " ";
        i++;

        if (i % 10 != 0) continue;

        std::cout << endl;

        if (i >= 100) done = true;
    } 
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;
unsigned int 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.
It looks to me like the condition for your loop is wrong. What you seem to want here is:
 
while(this->readConfigValue(key)) != value.isEmpty())

Or something like that. Some jerk burned to coffee pot this morning so I'm a little slow.
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;
    unsigned int 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.)
I hadn't thought of that! Beautiful, cire.
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.

Thanks.
closed account (z05DSL3A)
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.

http://www.cprogramming.com/c++11/c++11-lambda-closures.html
Topic archived. No new replies allowed.