Yes there is a similarity but there is also difference
break
- will stop a loop and switch condition. Can be used only for switch, and loop statements
return
- will finish the function execution but the statements below of this keyword will not be executed. Can be used only for any functions.
Usage of return
keyword in void function
If you use return
in a void function like this
void trySomething()
{
Log.i("Try", "something");
return;
Log.e("Try", "something");
}
the execution of this function is done but the statement(s) below will not be executed.
Usage of break
keyword
for any loop statements
void tryLoop()
{
while(true)
{
Log.d("Loop", "Spamming! Yeah!");
break;
}
}
the loop will be stopped and continue the remaining statements of this function
for switch condition
void trySwitch()
{
int choice = 1;
switch(choice)
{
case 0:
Log.d("Choice", "is 0");
break;
case 1:
Log.d("Choice", "is 1");
case 2:
Log.d("Choice", "is 2");
}
}
using break
in switch condition is also same as loop. Omitting the break
will continue the switch condition.