About Lesson
Goto is an unconditional jump statement that transfers the control to a line marked by a label. Goto is never necessary and in practice rarely used, as it makes the code hard to understand and maintain.
Still, there are a few scenarios where we could use goto, like in a deeply nested structure, such as breaking out of multiple loops at once (since break only exits the innermost loop).
while (...) while (...) while (...) { ... if (error) goto error_handler; ... } ... error_handler: /* handle error here */
This is useful if error handling code is non-trivial and errors can occur in several places.
A label has the same form as a variable name and its scope is the entire function. In the aforementioned example,
goto error_handler;
transfers the control to the line with label “error_handler”.
error_handler:
A label is followed by a colon.