Return和Break语句之间的区别

时间:2011-07-08 06:54:12

标签: java android

return语句与break语句有何不同?
如果我必须退出if条件,我应该选择哪一个returnbreak

14 个答案:

答案 0 :(得分:86)

break用于退出(转义)您当前正在执行的for - 循环,while - 循环,switch - 语句。

return将退出当前正在执行的整个方法(并且可能会向调用者返回一个值,可选)。

因此,要回答您的问题(正如其他人在评论和答案中所述),您无法使用breakreturn来逃避if-else - 声明本身。它们用于逃避其他范围。


考虑以下示例。 x - 循环中while的值将决定循环下面的代码是否会被执行:

void f()
{
   int x = -1;
   while(true)
   {
     if(x == 0)
        break;         // escape while() and jump to execute code after the the loop 
     else if(x == 1)
        return;        // will end the function f() immediately,
                       // no further code inside this method will be executed.

     do stuff and eventually set variable x to either 0 or 1
     ...
   }

   code that will be executed on break (but not with return).
   ....
}

答案 1 :(得分:53)

如果要退出循环,则使用

break,而return用于返回调用它的步骤或停止进一步执行。

答案 2 :(得分:11)

您将无法使用ifreturn退出break条件。

return用于需要在方法执行完毕后从方法返回/您不想执行其余方法代码时。因此,如果您使用return,那么您不仅会从if条件返回,还会从整个方法返回。

考虑以下方法 -

public void myMethod()
{
    int i = 10;

    if(i==10)
        return;

    System.out.println("This will never be printed");
}

这里,使用return导致在第3行之后停止执行整个方法并执行返回其调用者。

break用于突破loopswitch声明。考虑这个例子 -

int i;

for(int j=0; j<10; j++)
{
    for(i=0; i<10; i++)
    {
        if(i==0)
            break;        // This break will cause the loop (innermost) to stop just after one iteration;
    }

    if(j==0)
        break;    // and then this break will cause the outermost loop to stop.
}

switch(i)
{
    case 0: break;    // This break will cause execution to skip executing the second case statement

    case 1: System.out.println("This will also never be printed");
}

此类break语句称为unlabeled break语句。还有另一种形式的休息,称为labeled break。考虑这个例子 -

int[][] arrayOfInts = { { 32, 87, 3, 589 },
                            { 12, 1076, 2000, 8 },
                            { 622, 127, 77, 955 }
                          };
int searchfor = 12;

int i;
int j = 0;
boolean foundIt = false;

search:
    for (i = 0; i < arrayOfInts.length; i++)
    {
        for (j = 0; j < arrayOfInts[i].length; j++)
        {
            if (arrayOfInts[i][j] == searchfor)
            {
                foundIt = true;
                break search;
            }
        }
    }

此示例使用嵌套for循环来搜索二维数组中的值。找到该值后,标记的中断将终止外部for循环(标记为“search”)。

您可以从JavaDoc了解更多来自breakreturn的语句。

答案 3 :(得分:8)

没有冒犯,但其他答案(到目前为止)没有一个是正确的。

break用于立即终止for循环,while循环或switch语句。您不能break阻止if

return用于终止方法(并可能返回一个值)。

任何循环或块中的return当然也会立即终止该循环/块。

答案 4 :(得分:4)

break: - 这些转移语句绕过正确的执行流程到外部 通过跳过剩余的迭代来获取当前循环

class test
{
    public static void main(String []args)
    {
        for(int i=0;i<10;i++)
        {
            if(i==5)
            break;
        }
        System.out.println(i);
    }
} 

output will be 
0
1
2
3
4

继续: - 这些转移语句将绕过执行流程到循环的起始点,以便通过跳过所有剩余的指令继续下一次迭代。

class test
{
    public static void main(String []args)
    {
        for(int i=0;i<10;i++)
        {
            if(i==5)
            continue;
        }
        System.out.println(i);
    }
} 

output will be:
0
1
2
3
4
6
7
8
9 

返回: - 在方法中的任何时候,return语句都可以 用于导致执行分支回到方法的调用者。 因此,return语句立即终止其中的方法 它被执行了。以下示例说明了这一点。这里, return导致执行返回Java运行时系统, 因为它是运行时系统调用main()。

class test
{
    public static void main(String []args)
    {
        for(int i=0;i<10;i++)
        {
            if(i==5)
            return;
        }
        System.out.println(i)
    }
} 


output will be :
0
1
2
3
4

答案 5 :(得分:3)

Break语句将中断整个循环并在循环后执行代码,Return将不会在return语句之后执行代码并执行下一个增量循环。

<强>额

for(int i=0;i<5;i++){
    print(i)
    if(i==2)
    {
      break;
    }
}

输出:0 1

<强>返回

for(int i=0;i<5;i++)
{
   print(i)
   if(i==2)
   {
    return;
   }
}

输出:0 1 3 4

答案 6 :(得分:2)

break打破当前循环并继续,而return它会破坏当前方法并从您调用该方法的地方继续

答案 7 :(得分:2)

正如其他人已经指出的那样,返回将退出该方法。如果你需要跳过方法的某些部分,你可以使用break,即使没有循环:

label: if (some condition) {
    // some stuff...
    if (some other condition) break label;
    // more stuff...

}

请注意,这通常不是好的风格,但有时很有用。

答案 8 :(得分:2)

使用 break 打破循环或switch语句。

您在函数中使用 return 来返回值。 Return语句结束函数并将控制返回到调用函数的位置。

答案 9 :(得分:0)

在这段代码中,i迭代到3然后循环结束;

int function (void)
{
    for (int i=0; i<5; i++)
    {
      if (i == 3)
      {
         break;
      }
    }
}

在这段代码中,我被迭代到3但是有一个输出;

int function (void)
{
    for (int i=0; i<5; i++)
    {
      if (i == 3)
      {
         return i;
      }
    }
}

答案 10 :(得分:0)

打破只是打破循环&amp; return将控制权返回给调用者方法。

答案 11 :(得分:0)

return语句与break语句有何不同? Return语句退出当前方法执行并将值返回给调用方法。 Break用于退出任何循环。

如果我必须退出if条件,我应该选择哪一个,返回或中断?

要退出方法执行,请使用return。 要退出任何循环,您可以根据您的要求使用中断或返回。

答案 12 :(得分:0)

中断只会停止循环,而在循环内部返回则将停止循环并从函数返回。

答案 13 :(得分:-1)

如果要退出简单的if else语句但仍保留在特定上下文中(而不是返回到调用上下文),则可以将块条件设置为false:

if(condition){
//do stuff
   if(something happens)
        condition = false;
}

这将保证没有进一步的执行,我认为你想要的方式。你只能在loopswitch case

中使用中断