Branch Statements In Java : Introduction
- There are statements which are used to skip some parts of program and branch the control to another area. These are called branch statements in java.
- There are preferably two type of branch statements in java :
- We will try to give an idea about both of these branch statements In java along with their respective flowcharts and examples.
1) Branch Statements In Java : The Break Statement
- Java break statement is used to come out of loop (break from loop) and from switch block.
- It is handy to use in situations when we want to break the loop in between without full completing the iterations.
- We have already seen the use of break statement in switch. Now let’s use break in loop. Below is the mentioned flowchart and example for it.
Note : "Break" can be used with any loop.
- As we can see in the output that when value of i=2 the loops breaks and control comes out of the loop.
Note : If we use nested loop and we have break statement in the inner loop then only the inner loop breaks.
2) Branch Statements In Java : The Continue Statement
- Java continue statement skips the part of program after continue statement and continues the flow of program. It is used to continue the loop.
- Below is the mentioned flowchart and example for it.
- You can see in the above output : 2 is not printed because when i=2 the continue statement does it work and skips the remaining statements and continues the loop.
Note : When using continue in nested loops , if continue is used is inner loop the only inner loop is continued , no effect of continue in outer loop.