3 ms·
Can you suggest a use case, assuming modern best practices, where this would improve readability/understandability and ease of testing? That kind of control flo
by smitherfield 11y ago
Can you suggest a use case, assuming modern best practices, where this would improve readability/understandability and ease of testing? That kind of control flow looks a bit scary to me.
- ars 11y agoSay you are processing CSV files line by line (outer loop), column by column (inner loop). You come across something in the data the means stop processing that line and skip to the next one. So you do 'continue 2;' to skip the inner loop, and anything after it (presumably the code that would do something with all those columns you just got) and simply start a new line. There are lots of use cases. Obviously there are other ways: You can use a goto, you can set flag variables, you can use functions in the inner loop to give you a way to exit early. But this type of control flow is easiest to read. Many other language have this BTW. C doesn't. It's also good for error handling. If you find an error in the inner loop how do you exit that loop, and the outer one? You would do 'break 2;' and exit both loops. The labeled version is because using a number is prone to errors if someone changes how many loops there are. Do a search for 'goto' in high quality code and see how many of them can be replaced by this - it's the vast majority.
- kruczek 11y agoBut why? I mean, if I understand correctly, it would be exactly like goto.. so, why not use goto instead? You could also have named labels to clearly describe where you want to jump and why.