Giving name to a loop

前端 未结 6 1772
北荒
北荒 2020-12-06 05:33

While browsing questions and answers in this forum i found a piece of code were names were given to loops in order to use them for break. Like

nameofloop:
          


        
6条回答
  •  执念已碎
    2020-12-06 05:42

    This is not a labeled loop, is just a label that you place anywhere and then you can "break" or "continue" to depending on your conditions. You can also use in a nested if-else with for loopings in order to break several loops decorated with if-else, so you can avoid setting lot of flags and testing them in the if-else in order to continue or not in this nested level.

    Its use is discouraged as resembles a goto and causes spaghetti-code.

    Personally I used only once, time ago, in order to break a for loop inside other two for loops with if-else and continue in the outer loop, as break inside a loop breaks this loop, but you continue in the outer loop, not the most-outer that was my case.

提交回复
热议问题