h a l f b a k e r y"Look on my works, ye Mighty, and despair!"
add, search, annotate, link, view, overview, recent, by name, random
news, help, about, links, report a problem
browse anonymously,
or get an account
and write.
register,
|
|
|
If you really nest deeply into loops, sometimes you just want to get out of them. So this introduces two new keywords
breakbreak;
and
breakbreakbreak;
These break out of two and three loops
They can be combined as they support the + operator.
breakbreak+breakbreak;
and
breakbreakbreak+breakbreak;
will
break out of four and five loops at once.
Nim Break Statement
https://nim-lang.or...nts-break-statement [Cuit_au_Four, Apr 04 2019]
Please log in.
If you're not logged in,
you can see what this page
looks like, but you will
not be able to add anything.
Destination URL.
E.g., https://www.coffee.com/
Description (displayed with the short name and URL.)
|
|
What language is this in? Also, why can't you just have a
command that breaks out of all the loops currently running? |
|
|
Some languages allow you to jump to a label. C provides the goto
statement to let you jump wherever you want in the function. In Nim,
the break statement can be passed a label to specify which block to
break out of. |
|
|
// C provides the goto statement // |
|
|
True, but its actual use - other as an example of How Not To Program - is punishable by death (for a first offence). |
|
|
I've never understood why people are so averse to GOTO. I
mean apart from the obvious problem of code followability. |
|
|
I don't want to specify al label, where i'm breaking out of. |
|
|
it's the equivalent of ending up on the bad side of youtube when you just want to go BACK and you don't care where to. Just BACK or BACK BACK if that's not enough.
Just to get away. |
|
|
In that case, you don't want 'break' - you want 'brexit' ... |
|
|
Nonono. That would take forfuckingever. |
|
|
Some languages have this already (such as PHP) using a simpler syntax: break 3; will break out of 3 nested loops. |
|
|
In C, breaking out of nested loops is a perfectly legitimate use of goto. Some people go to great lengths to avoid using goto, sometimes to such an extreme that their code ends up looking much messier than it would have been. |
|
|
A slightly less controversial approach is to stick your loops in a subroutine, and return from it when you want to break. |
|
|
It really wouldn't take much work to write a simple function, say 'break(n)' where 'n' is the number of stack entries to be unwound. I think C lets you do it explicitly (check out 'set jmp') |
|
|
'Breaker... breaker...219...' |
|
|
The reason this is a bad idea is that it betrays an
uncertainty over flow. |
|
|
The only thing wrong with goto is that bad programmers use it. That's a good reason, but still insufficient to make a rule absolutely forbidding it. I've had cases where a goto would have literally saved me 40 lines of code. |
|
|
I think this is an object lesson as to why you should not code or visit web pages while sipping gin. |
|
|
Indeed; a mug of Red Bull with a couple of nicotine patches dropped in it is much more ... stimulating. Until the tachycardia disabled you, that is. |
|
|
We offer in evidence an example of doubtful coding from the early years of 8-bit microprocessors. |
|
|
Tasked with writing an auto-ranging routine for a switched-gain electrometer amplifier, an electronic engineer and self-taught assembly language programmer adopted an eccentric solution. |
|
|
This consisted of making the autoranging routine recursive, but incorporating a "depth counter". If the recursion cot too "deep" (for the 8-bit pointer, exceeding 0xFC), the "error handler" would search down the processor's stack (not by POP/PUSH - that would be too boringly conventional - but by indexed addressing the relevant RAM page directly) and identify the return address for the module until it found data that wasn't a return address. |
|
|
Then it would alter the processor stack pointer to that location, PUSH the address of the amplifier zero-calibration routine onto the stack, reset the CPU flags register and execute an RTS. |
|
|
Some sort of GOTO would probably have been preferable.
We wish to express our sincere hope that the author of the code is still alive, but not well. In fact, he ought to be suffering from an excruciatingly painful, debilitating, disfiguring and humiliating medical condition, for which there is no cure and not even an effective palliative treatment. Rather like his code, actually. |
|
| |