Difference between Return and Break statements
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
?
Solution 1:
break
is used to exit (escape) the for
-loop, while
-loop, switch
-statement that you are currently executing.
return
will exit the entire method you are currently executing (and possibly return a value to the caller, optional).
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.
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:
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).
....
}
Solution 2:
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.
Solution 3:
You won't be able to exit only from an if
condition using either return
or 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.
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.
break
is used to break out from a loop
or a switch
statement. Consider this example -
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 -
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").
You can learn more abour break
and return
statements from JavaDoc
.