C has a goto statement and labels, so you can branch about the way you used to. But most of the time goto's aren't needed. (How many have we used up to this point?) The code can almost always be more clearly expressed by for/while, if/else, and compound statements.One use of goto's with some legitimacy is in a program which contains a long loop, where a while
would be too extended. Then you might write
mainloop:
...
goto mainloop;
Another use is to implement a break
out of more than one level of for
or while
. goto's can only branch to labels within the same function.there are a few situations where gotos may find a placeThe most common is to abandon processing in some deeply nested structure, such as breaking out of two or more loops at once. The break statement cannot be used directly since it only exits from the innermost loop.
0 comments:
Post a Comment