3 ms·
"break" and "continue" are both effectively a "goto", and they're pretty commonly used.
by phantom784 3y ago
"break" and "continue" are both effectively a "goto", and they're pretty commonly used.
- Jtsummers 3y agoThey are only equivalent to a very limited goto, not the unlimited sort that Dijkstra's letter was primarily about. A continue or break can be replaced with this: while (some_cond) { if (whatever) continue; if (whocares) break; } => while (some_cond) { next: if (whatever) goto next; if (whocares) goto after; } after: ... EDIT: added break into the above, forgot to earlier Note that it stays within the same scope or, in the case of a break, would just escape that scope. The sort of goto Dijsktra wrote about would let you do things like this: void foo() { while(some_cond) { next: // how did we get here again? ... } } void bar() { goto next; }
- shagie 3y agothrow, raise, catch, switch... even if and else. They're all wrappers around goto with various amounts of safety in their handling. Some implementations are more safe than others ( https://www.php.net/manual/en/control-structures.break.php https://www.php.net/manual/en/control-structures.break.php still gives me the heebie jeebies thought it was worse in older versions where the number could be a variable rather than an integer literal https://web.archive.org/web/20140312123653/https://www.php.net/manual/en/control-structures.break.php https://web.archive.org/web/20140312123653/https://www.php.n... )
- pyrale 3y agoMany control flow instructions can be expressed as other instructions. The idea of the article was to promote higher level, less ambiguous and less ubiquitous instructions. Sure, you can express a for-loop in terms of goto. But when you read a for-loop, there are many things you're guaranteed won't happen that could with a goto. In that sense, it takes less attention to make a good for-loop. And a for-loop iterating over a collection also takes less attention than one with a manually-crafted iteration check. That's the whole point of the article: lower-level instructions should be wrapped in higher-level instruction, more specialized and with less footguns.