java2db

Branching or break continue and return statements in java



Branching statements ( break continue and return) are very essential to control the loop and switch control flow statements.

break continue and return break statement
break is used to terminate the loop.
break is used inside the switch block to come out of the switch block.
break can be used in nested loops to skip the current loop execution.
break continue and returncontinue statement
continue statement is used to repeat the next iteration of the loop.
When continue is executed, subsequent statement in the loop are not executed and control of execution goes back to the  next repetition of the loop.
break continue and return return statement
Method will return the result in the form of a data type,class object or a collection.
The data type of the returned value must match the type of the method’s declared return value .

 

Syntax for break continue and return:

while(Condition)
{
—————–//statements
if(condition)
{
break;
}
}
while(Condition)
{
—————–//statements
if(condition)
{
continue ;
}
}
modifier returnType MethodName(Parameters)
{
—————-
—————-
//statements
return resultType ;
}

Example for break  :

If the condition (if(i==1 && j==1))  is true ,  the break statement will terminates the current (inner ) loop only.

Example for continue  :

If the condition (if(i==1 && j==1)) is true ,  the continue statement will continues the next iteration of the same (inner) loop.

Example for return :

If the condition (if(i==1 && j==1)) is true , the return statement  terminates the entire execution .

General example for break continue and return:

Execution Result:

break continue and return  We can use labeled break to come out from the nested loop.
break continue and return Labeled continue statement helps to start the next iteration from the outer loop.
break continue and return The return type of the method and returning result should be the same data type.




Related Posts :
How to use for loop control flow statement in java
How to use while and do while loop control flow statements in java
   [will not be published]



^ <