Jump Statements

These will transfer the control to another part of the program.

Break

Continue

Return.

  • Break

The uses of this statement are

  1. To stop the sequence of statements execution in switch control statement.

  2. To exit from the loop.

  3. Civilization version of goto.

2.

 //example for break statement to exit a loop
public class BreakExample1{

 public static void main(String []args){
 for(int i=1;i<10;i++)
 {
 if(i==5)
 break;

 System.out.print(i+"");
 }
 System.out.println();
 }
}

output:

1 2 3 4

like wise we can use break in while loop also.

when we uses break inside the nested loops then it will terminated from the innermost loop it will continue with the outer loop.

// example for using break statement in nested loops.
public class BreakExample2
{

 public static void main(String []args){
 for(int i=1;i<4;i++)
 {
 System.out.println("i value is"+i+"");
 for(int j=1;j<3;j++)
 {
 if(j==2)
 break;

 System.out.println("j value is"+j+"");
 }

}
 }
}

Output:

i value is 1

j value is 1

i value is 2

j value is 1

i value is 3

j value is 1

Here we uses break in outer loop.

// example for using break statement in nested loops.
public class BreakExample3
{

 public static void main(String []args){
 for(int i=1;i<4;i++)
 {
 if(i==2)
 break;
 System.out.println("i value is"+i+"");
 for(int j=1;j<3;j++)

 System.out.println("j value is"+j+"");

 }

 }
}

Output:

i value is 1

j value is 1

j value is 2

The break statement in switch statement will affects only that statements not enclosed loop.

3.

we can use break as go to by specifying the label name where that execution should be resume.

Syntax:

break label;

when it executes it will terminate from the named label.

//example for using break as goto
public class BreakAsGoto
{

 public static void main(String []args){

 boolean t = true;
 first: {
 second: {
 third: {
 System.out.println("Before the break.");
 if(t) break second; // break out of second block
 System.out.println("This won't execute");
 }
 System.out.println("This won't execute");
 }
 System.out.println("This is after second block.");
 }
}
}

Output:

Before the break.

This is after second block.

  • Continue

It allows us to stop the execution of a loop and executes the start of the loop.

In while and do-while loops, it causes control to be transferred directly to the conditional expression that controls the loop.

In a for loop, control goes first to the iteration portion of the for statement and then to the conditional expression.

For all three loops, any intermediate code is bypassed.

//example for continue statement
public class HelloWorld{

     public static void main(String []args)
     {
       for(int i=1; i<10; i++)
       {
         System.out.print(i + " ");
          if (i%2 == 0) continue;
          System.out.println("");
        }
     }
}

Output:

1

2 3

4 5

6 7

8 9

like break we can use label with continue also.

// Using continue with a label.
class ContinueLabel {
 public static void main(String args[]) {
outer: for (int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
 if(j > i) {
 System.out.println();
 continue outer;
 }
 System.out.print(" " + (i + j));
 }
 }
 System.out.println();
 }
}

Output:

0

1 2

2 3

  • Return

    At any time in a method, the return statement can be used to cause execution to branch back to the caller of the method. Thus it immediately return back from the executing method.

//example for return statement
public class ReturnExample{

     public static void main(String []args)
     {
       boolean t = true;
 System.out.println("Before the return.");
 if(t) return; // return to caller
 System.out.println("This won't execute.");

     }
}

Output:

Before the return.

if(t) is mandatory as java compiler should know that last println() statement will not execute otherwise it will show unreachable code error.

results matching ""

    No results matching ""