Search

Saturday, April 10, 2021

Java Break & Continue Statement

 



Java Break Statement


If we want to stop the execution of loop at specific point then we can use break statement in loop. When Java reads the break statement inside the loop, it will stop the execution of the loop based on the condition.


For Example


class BreakTest

          {

          public static void main (String[] ar)

          {

          for ( int n=1; n<=10; n++)

{

          If (n==5)

                   {

                   break;

                   }

                    System.out.println (n);

                    }

}

}


Output

1234

 


Continue Statement in Java


The continue statement in Java language is used to stop the execution of just one iteration of loop if condition is true and it remains continue up to loop ending point.

 

For Example


class ContinueStatementCheck

{

          Public static void main (String[] ar)

          {

                   for (int j= 0; j<=5; j++)

                   {

                             If (j==3)

                             {

                                      Continue;

                             }

                   System.out.println(j);

                   }

          }

}


Output:

1245


No comments:

Post a Comment