5 ms·
Well, sometimes they are useful when a break statement is not enough to get you out of a nested loop. while (...) { while (...) { // ...
by stabbles 7y ago
Well, sometimes they are useful when a break statement is not enough to get you out of a nested loop.
while (...) {
while (...) {
// ...
if (...)
goto stop_it;
// ...
}
// ...
}
stop_it:
- amitport 7y agothis use case is specifically addressed by "labeled statements" (e.g., https://docs.oracle.com/javase/specs/jls/se12/html/jls-14.html#jls-14.7 https://docs.oracle.com/javase/specs/jls/se12/html/jls-14.ht...)
- mFixman 7y agoDouble-breaking is a good sign that you need to refactor your code and put any of the while statements inside its own function.
- pjc50 7y agoThat generally makes it harder to follow and harder to optimise?
- Joky 7y agoQuite the opposite IMO: it gives a name to the computation and simplifies the control flow by making the computation more functional.
- kyllo 7y agoIn a high level language, yes, you should probably refactor code that looks like that. In C, no--just write the nested loop that iterates the pointers over the array, and use break or goto.
- burfog 7y agoWell then you'll need double-returning, which isn't generally available. One could use longjmp I suppose.
- chrisbrandow 7y agoI don’t know if it’s a semantic difference or not but some languages, like swift now have labeled loops so the break statement can reference the specific loop, allowing control flow to jump out of the nest.
- kevin_thibedeau 7y agoNothing new there. Labeled loop exits are a feature of Ada.
- pjc50 7y agoThe "labelled goto" of the modern world is very far from the GOTO of pre-Dijkstra, which would allow you to jump into the middle of loops or functions.
- Someone 7y agoAlso: “everything in moderation”. If over half your control flow statements are GOTOs, things get hairy fairly soon. For an example, look up ancient Fortran code.
- AnimalMuppet 7y agoYeah. Structured programming was a huge improvement over the... let's call it "unstructured" programming that preceded it. Essentially, you take any flowchart you can possibly draw, and you can implement it with GOTOs. The result wasn't easy to understand, debug, or maintain, though. One question, though: Of the current languages that have a GOTO, won't some of them still allow you to jump into the middle of a loop? That is, is it the languages that force us not to do that any more? Or is it just that we've learned not to do that, because it's a horrible idea?
- tom_ 7y agoDuff's device is sort-of an example of this, as cases in C are equivalent to goto labels. It's an interesting point, though. I don't think I've ever seen anybody actually use gotos for this, and I don't remember ever doing it myself. (I will happily put a goto label anywhere that will compile - I'm not against goto in principle.) I'd assume it's because 99% of the time there's always some other non-goto option that could be used, and C programmers are accustomed to thinking about their programs in terms of these constructs, rather than necessarily goto. goto is always there when all else fails - but when it comes to loops, all else fails rather rarely. Some (non exhaustive) examples. Just thinking about the loop mechanics: jumping into the middle of a C for loop probably isn't super-useful, as you're only skipping the one-time init, which you could skip anyway: just leave it empty and put that code elsewhere. Then maybe turn the loop into a while loop? - that's an option. Anyway, for and while are basically equivalent for the purposes of this discussion. Jumping into the body of a do...while makes it a while, and jumping into the middle of a while makes it a do...while. So C programmers would just write the right sort of loop in the first place. goto X; ---> do { while(cond) { A; X: } while(cond); A; } goto X; ---> while(cond) { do { A; A; } X: } while(cond) And regarding the body: jumping halfway into the body is equivalent to one of those annoying loops where the setup for the condition is rather involved. One option here is an infinite loop with a break in the middle, so C programmers might just write the loop that way in preference to the one with a goto. goto X; ---> for(;;) { while(cond) { B; A; if(!cond) break; X: A; B; } } The do...while case is equivalent. goto X; ---> for(;;) { do { B; A; if(!cond) break; X: A; B; } } while(cond); Or another option is to pop B in a function, that checks the condition. while(B()) { A; }
- Vaskivo 7y agoYes. Lua doesn't have continue or break in loops. But it has goto. That's one of the uses of it. It also has, IMO, a sane goto. You cannot jump to any label outside of the function you are running from. https://www.lua.org/manual/5.3/manual.html#3.3.4 https://www.lua.org/manual/5.3/manual.html#3.3.4
- earenndil 7y agoC goto can't jump to another function either (except for longjmp, but that's a whole other story).