• Advertisement
Sign in to follow this  

How do I do this without using goto statements?

This topic is 3524 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Wow! A double break! That's a cool idea! I wrote my own compiler that's roughly C/C++. I might add that. It's funny -- I learned there was a "continue" statement in C/C++. I had never used it or seen it. I added it, just kinda guessing what it might do. (Too lazy to check a standard compiler.)

Share this post


Link to post
Share on other sites
Advertisement
Losethos: from my language design experience, adding features to a language because they are "cool" is an excellent way to get an overly bloated and badly fitting language. Always consider the many alternatives that have already been invented by other language designers before rolling out your own. A good example would be Java's labeled break statements, which are by far superior to anything "double break" may mean.

Share this post


Link to post
Share on other sites
Quote:
Original post by ManaStone
I really wish C++ had the ultimate break that would get out of every loop.
That's exactly what your goto statement did. It jumps out of your loop to another address, just like a break would do.

What's the big deal? You must have read too deep into someone's anti-goto sentiments. That rule gets hammered into people way too much. People learn how to properly write functions and classes these days, we aren't 6 months out from people writing oldschool 'goto linenumber' BASIC code.

The only bad thing that can happen is that you create spaghetti code that is impossible to follow. You haven't done that here. It's a perfectly valid flow control statement.

Share this post


Link to post
Share on other sites
ToohrVyk,

On second thought, I removed the double_break. Thanks for talking me out of it. Goto is fine and actually clearer since the label catches your eye -- no need to bloat the language.

Share this post


Link to post
Share on other sites
The double_break added a bunch of parameters to internal compiler routines, slowing it down... Thanks to you, I was inspired to remove the "continue" statement too! I don't use that crappy thing. If I were a teacher, "goto" would be fine in very limited circumstances but that "continue" statement would be banned because no real-world programmers use it! Let's take a poll... anybody use the C/C++ "continue" statement? Screw that--keep my compiler lean.



Share this post


Link to post
Share on other sites
Quote:
Original post by losethos
Let's take a poll... anybody use the C/C++ "continue" statement? Screw that--keep my compiler lean.


I do, more than break actually (you can break out of a loop by correct use of the loop condition, but selectively dropping out of a loop iteration before it's over is harder).

As far as 'continue' goes, it should be fairly easy to implement on most code generation architectures with at most a single additional parameter: it is usually implemented as a "goto end;" with a fresh "end:" label being added automatically at the end of the loop body. It can even be done at parsing time!

Share this post


Link to post
Share on other sites
I do occasionally, why not? I tend to take opinions lightly when someone says DONT EVER DO X, simply because it is situation dependant.

Share this post


Link to post
Share on other sites
This doesn't really give enough context to find a solution. One, perfectly valid solution is this:
try {
while(condition1())
{
func1();
while(condition2())
{
func2();
if(condition3()) throw std::exception("Some error");
}
func3();
}
} catch (std::exception &e) {
}
func4();


If condition3 represents an error, then exceptions are what can be used.

The core of the problem here lies simply in that individual functions do not encapsulate functionality well, since they need to manipulate caller's context.

But all of this is mostly pointless. *Which activity?* Tweaking a solution doesn't bring many benefits if the solution itself is unsuitable.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement