break – continue – return – System.exit(0)

The break keyword is used to breaks(stopping) a loop execution, which may be a for loop, while loop, do while or for each loop.

The break statement can also be used to jump out of a loop.

for (int i = 0; i < 10; i++) {

 if (i == 4) {

 break;

}

System.out.println(i);

}

This example jumps out of the loop when i is equal to 4:

0, 1, 2, 3

The continue keyword is used to skip the particular recursion only in a loop execution, which may be a for loop, while loop, do while or for each loop.

The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.

for (int i = 0; i < 10; i++) {

if (i == 4) {

continue;

 }

System.out.print(i);

}

This example skips the value of 4:

0,1,2,3,5,6,7,8,9

 

The return statement stops the execution of a function and returns a value from that function. It is also used to exit from a method, with or without a value.

int myFunction(int a, int b)

{
  return a * b;

void RR(int i)

{

    if (i < 9)

         return;  //   control exits the method if this  condition(i.e, j<9) is true.  

    ++i;  

}

System.exit(0) when there is an abnormal condition and we need to exit the program immediately from a place other than the main method.

for(i = 0; i < 3; i++) {

      if(arr2[i] > = 20) {

            System.out.println(“exit…”);

            System.exit(0);

      } else {

            System.out.println(“arr2[“+i+”] = ” + arr2[i]);

            }

      }

 

 

This entry was posted in Basic. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *