BRANCH STATEMENTS


Branch Statements In Java | Core Java Tutorial | Minigranth

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 :
    • Break.
    • Continue.
  • 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.

    Flowchart
    This image describes the flowchart of the break statement of branch statements in java.
    The Break Statement : Flowchart

    Example
    This image describes the sample program of break statement in java.
    The Break Statement : Example

    This image describes the output of the sample program of break statement in java.
    The Break Statement : Output
    • 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.

    Flowchart
    This image describes the flowchart of the continue statement of branch statements in java.
    The Continue Statement : Flowchart

    Example
    This image describes the sample program of continue statement in java. The Continue Statement : Example

    This image describes the output of the sample program of continue statement in java.
    The Continue Statement : Output

    • 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.