devxlogo

Using goto to Exit a Nested Loop

Using goto to Exit a Nested Loop

Loops (for, while, do..while) are used often in code?especially nested loops. Eventually, you need to exit the loop by using a “break” statement. However, if you are inside of a nested loop, “break” will only let you exit one level of a loop. To exit directly, you can use “goto” to a label and exit even from a deeply nested loop.
Example:

 for(int i = 0; i 

size=3>

devx-admin

Share the Post: