Return和Break语句之间的区别

And*_*lva 59 java

如何做一个return声明,从不同break的语句?
如果我必须退出if条件,我应该选择哪一个,return或者break

Ava*_*vra 98

break用于退出(转义)当前正在执行的for-loop,while-loop,switch-statement.

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

因此,要回答你的问题(如他人的意见和答案已经注意到),你不能使用或者break也没有return逃脱的if-else语句来本身.它们用于逃避其他范围.


请考虑以下示例.-loop 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).
   ....
}
Run Code Online (Sandbox Code Playgroud)

  • 注意:`break`也结束`switch`。“ continue”将直接进入“ for”或“ while”的下一个循环。 (2认同)

Ova*_*tri 58

break当您想要退出循环时return使用,同时用于返回调用它的步骤或停止进一步执行.


MD *_*med 12

您将无法仅if使用return或中的某个条件退出break.

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

考虑以下方法 -

public void myMethod()
{
    int i = 10;

    if(i==10)
        return;

    System.out.println("This will never be printed");
}
Run Code Online (Sandbox Code Playgroud)

这里,使用returncause在第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");
}
Run Code Online (Sandbox Code Playgroud)

这种类型的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;
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

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

您可以从中了解更多信息breakreturn陈述JavaDoc.


Boh*_*ian 8

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

break用于立即终止for循环,while循环或switch语句.你不能break从一个if街区.

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

return任何循环或块内的A 当然也会立即终止该循环/块.


小智 5

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
Run Code Online (Sandbox Code Playgroud)

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

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 
Run Code Online (Sandbox Code Playgroud)

return :- 在方法中的任何时候,都可以使用 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
Run Code Online (Sandbox Code Playgroud)

  • 澄清 return|break|continue 之间差异的好例子。非常感谢!只有 1 件事,`System.out.println(i);` 应该在 for 循环内,顺便说一句:) (3认同)

小智 5

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

休息

for(int i=0;i<5;i++){
    print(i)
    if(i==2)
    {
      break;
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:0 1

返回

for(int i=0;i<5;i++)
{
   print(i)
   if(i==2)
   {
    return;
   }
}
Run Code Online (Sandbox Code Playgroud)

输出:0 1 3 4