Java Return 和 Break 语句之间的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6620949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between Return and Break statements
提问by Andro Selva
How does a return
statement differ from break
statement?.
If I have to exit an if condition, which one should I prefer, return
or break
?
如何做一个return
声明,从不同break
的语句?
如果我必须退出 if 条件,我应该更喜欢哪一个,return
还是break
?
采纳答案by Ovais Khatri
break
is used when you want to exit from the loop, while return
is used to go back to the step where it was called or to stop further execution.
break
当您想退出循环时return
使用,而 while用于返回到调用它的步骤或停止进一步执行。
回答by MD Sayem Ahmed
You won't be able to exit only from an if
condition using either return
or break
.
您将无法仅if
使用return
或退出条件break
。
return
is used when you need to return from a method after its execution is finished when you don't want to execute the rest of the method code. So if you use return
, then you will not only return from your if
condition, but also from the whole method.
return
当您不想执行其余的方法代码时,在执行完成后需要从方法返回时使用。因此,如果您使用return
,那么您不仅会从if
条件返回,还会从整个方法返回。
Consider the following method:
考虑以下方法:
public void myMethod()
{
int i = 10;
if(i==10)
return;
System.out.println("This will never be printed");
}
Here, using return
causes to stop the execution of the whole method after line 3 and execution goes back to its caller.
在这里,使用return
原因在第 3 行之后停止整个方法的执行并且执行返回到它的调用者。
break
is used to break out from a loop
or a switch
statement. Consider this example -
break
用于从 aloop
或 aswitch
语句中跳出。考虑这个例子 -
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");
}
This type of break
statement is known as unlabeled break
statement. There is another form of break, which is called labeled break
. Consider this example -
这种类型的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;
}
}
}
This example uses nested for loops to search for a value in a two-dimensional array. When the value is found, a labeled break terminates the outer for loop (labeled "search").
此示例使用嵌套的 for 循环来搜索二维数组中的值。找到该值后,标记的 break 将终止外部 for 循环(标记为“搜索”)。
You can learn more abour break
and return
statements from JavaDoc
.
您可以从 中了解更多关于 aborbreak
和return
statements 的信息JavaDoc
。
回答by Avada Kedavra
break
is used to exit (escape) the for
-loop, while
-loop, switch
-statement that you are currently executing.
break
用于退出(转义)您当前正在执行的for
-loop、while
-loop、switch
-statement。
return
will exit the entire method you are currently executing (and possibly return a value to the caller, optional).
return
将退出您当前正在执行的整个方法(并可能向调用者返回一个值,可选)。
So to answer your question (as others have noted in comments and answers) you cannot use either break
nor return
to escape an if-else
-statement per se. They are used to escape other scopes.
因此,要回答您的问题(正如其他人在评论和答案中指出的那样),您既不能使用break
也return
不能逃避 -if-else
语句本身。它们用于转义其他范围。
Consider the following example. The value of x
inside the while
-loop will determine if the code below the loop will be executed or not:
考虑以下示例。-loopx
内部的值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).
....
}
回答by Eng.Fouad
break
breaks the current loop and continues, while return
it will break the current method and continues from where you called that method
break
中断当前循环并继续,而return
它将中断当前方法并从您调用该方法的位置继续
回答by Bohemian
No offence, but none of the other answers (so far) has it quite right.
没有冒犯,但其他答案(到目前为止)都没有完全正确。
break
is used to immediately terminate a for
loop, a while
loop or a switch
statement. You can not break
from an if
block.
break
用于立即终止for
循环、while
循环或switch
语句。你不能break
从一个if
块。
return
is used the terminate a method (and possibly return a value).
return
用于终止方法(并可能返回一个值)。
A return
within any loop or block will of course also immediately terminate that loop/block.
return
任何循环或块中的A当然也会立即终止该循环/块。
回答by Benny Tjia
If you want to exit from a simple if else
statement but still stays within a particular context (not by returning to the calling context), you can just set the block condition to false:
如果你想退出一个简单的if else
语句但仍然停留在一个特定的上下文中(而不是通过返回调用上下文),你可以将块条件设置为 false:
if(condition){
//do stuff
if(something happens)
condition = false;
}
This will guarantee that there is no further execution, the way I think you want it..You can only use break in a loop
or switch case
这将保证没有进一步的执行,我认为你想要的方式..你只能在一个loop
或switch case
回答by Dirk
Return will exit from the method, as others have already pointed out. If you need to skip just over some part of the method, you can use break, even without a loop:
正如其他人已经指出的那样,返回将从该方法中退出。如果您需要跳过方法的某些部分,即使没有循环,您也可以使用 break:
label: if (some condition) {
// some stuff...
if (some other condition) break label;
// more stuff...
}
}
Note, that this is usually not good style, though useful sometimes.
请注意,这通常不是好的风格,尽管有时很有用。
回答by erdomester
In this code i is iterated till 3 then the loop ends;
在这段代码中, i 被迭代到 3 然后循环结束;
int function (void)
{
for (int i=0; i<5; i++)
{
if (i == 3)
{
break;
}
}
}
In this code i is iterated till 3 but with an output;
在这段代码中,我被迭代到 3,但有一个输出;
int function (void)
{
for (int i=0; i<5; i++)
{
if (i == 3)
{
return i;
}
}
}
回答by Techbrick
break just breaks the loop & return gets control back to the caller method.
break 只是中断循环 & return 将控制权返回给调用者方法。
回答by DcodeChef
How does a return statement differ from break statement?.Return statement exits current method execution and returns value to calling method. Break is used to exit from any loop.
return 语句与 break 语句有何不同?。Return 语句退出当前方法执行并将值返回给调用方法。Break 用于退出任何循环。
If I have to exit an if condition, which one should I prefer, return or break?
如果我必须退出一个 if 条件,我应该选择哪一个,返回还是中断?
To exit from method execution use return. to exit from any loop you can use either break or return based on your requirement.
要退出方法执行,请使用 return。要退出任何循环,您可以根据需要使用 break 或 return。