Using return to exit a loop?

后端 未结 5 1209
悲哀的现实
悲哀的现实 2021-01-07 18:27

If I write a for, do, or while loop, is it possible to come out of this with the return keyword?

Eg:

class BreakTest 
{
public static void Main() 
{
         


        
5条回答
  •  花落未央
    2021-01-07 19:17

    Keep these in mind:

    1. You CAN DO this, but not recommended because it automatically exits from the current method of execution.
    2. Not useful when other statements after the loop are mandatory no matter the conditions (due to it's behaviour stated in the 1st point).
    3. Can create a ripple effect in your program i.e. attempt to solve one error/bug/fault gives rise to many new. This makes debugging a bit complex.

提交回复
热议问题