W
William Hughes
C has no multi-level break statement. Use a goto statement.
No, really.
It can also be argued (and undoubtedly will be) that if you find
yourself needing a multi-level break, your code is too complicated,
and you should restructure the code so it no longer needs it.
Well, it may be possible to restructure and
simplify the code, but there
are times when this type of structure is needed, e.g.
searching for an element is a three dimensional matrix.
You have to do three loops and end processing when
an element is found. Disguising what you are doing
will not make things magically simpler.
My own preference is a labeled break.
C does not have this construct so use a goto.
There is nothing wrong with branching.
(Programming would be very difficult without
a branch. It is unrestricted branching that
leads to trouble.)
Rules for goto
1. Use very sparingly
2. Never jump backward or into a loop
3 Only jump to the end of a loop
4. If you are optimizing code
ignore the rules [Recall the rules
of optimziation, 1. Dont do it
(for experts) 2. Don't do it yet]
(Note that rules 2 and 3 can be added to
a compiler as warnings, so a language change
is not needed.)
- William Hughes