Liste des Groupes | Revenir à cl c |
On 13/12/2024 12:12, bart wrote:
Some people don't like ordinary break either. There you could also suggest using convoluted logic, or using a function instead But here 'break' would be clearly be simpler and easier.I can only express my own opinion - if someone else doesn't like "break;", that's up to them.
><https://godbolt.org/z/7qWf966Er>
(I'd like to see a lambda example that is simpler than a two-level break or even a goto.)
There is a simple three-level nested loop function with an "escape" from the inside loop, written in several different ways. There is somewhat arbitrary calculations before and after the loops, so that the it can't just use a direct "return" from the middle of the loop - it needs some kind of multi-level break effect when "test(x, y, z)" passes.
The lambda version code is C++, since that has lambdas and C doesn't, but the rest can be compiled as C or C++.The lambda version needs closures from what I can see, although that might be optimised out.
(I've also used C++ tuples for returning multiple values.From that 'inner' function presumably; the main one returns one value.
I haven't compared to a multi-level break statement, because neither C nor C++ have such a feature (as yet).I've written a version in my syntax below, with some comments.
The other big advantage of the "static inner function" version (valid C) and the lambda version (C++ only - though lambdas are being considered for C) is that it is immediately obvious when writing them that there is a huge difference between breaking out of the loops when the test passes, and falling off the end of the loops because the test never passes. I've been lazy in this example, but you can't miss it - with the goto to break versions, it's easily forgotten.Do you mean this line:
return std::tuple(n, n, n);Because even without it, it seems to work for M=10 (when the break is commented out). That is, it gives the same result on versions that just fall off the end of the outer loop.
I'm saying that if you don't have the general feature, then the more limited one might be worth considering, as it will take care of most of the use-cases not covered by break.I've had a quick look through my codebases (not C), and I couldn't see an example of a numbered break. All I could find was the equivalant of:So you have a feature (numbered breaks) in your language that you never use, but have been recommending here as a useful addition to C?
>
break # the vast majority
break all
>
The latter breaks out of the outermost loop.
>
Les messages affichés proviennent d'usenet.