break

Sometimes we use Jumping Statements in Java. Using for, while and do-while loops is not always the right idea to use because they are cumbersome to read. Using jumping statements like break and continue it is easier to jump out of loops to control other a

break

Sometimes we use Jumping Statements in Java. Using for, while and do-while loops is not always the right idea to use because they are cumbersome to read. Using jumping statements like break and continue it is easier to jump out of loops to control other a

break

break

     

Sometimes we use Jumping Statements in Java. Using for, while and do-while loops is not always the right idea to use because they are cumbersome to read. Using jumping statements like break and continue it is easier to jump out of loops to control other areas of program flow.

We use break statement to terminate the loop once the condition gets satisfied. 
Lets see a simple example using break statement.

class BreakDemo{
  public static void main(String[] args) {
  for (int i = 0; i < 5; i++) {
  System.out.println(i);
  if (i==3) {
  break ;
  }
  }
  }
}

In the above example, we want to print 5 numbers from 0 to 1 at first using for loop as shown. Then we put a condition that 'if i = = 3', the loop should be terminated. To terminate the loop after satisfying the condition we use break. 
It gives the following output:

C:\javac>javac BreakDemo.java

C:\javac>java BreakDemo
0
1
2
3

The break statement has two forms: labeled and unlabeled. You saw the labeled form in the above example i.e. a labeled break terminates an outer statement. However, an unlabeled break statement terminates the innermost loop like switch, for, while, or do-while statement. 

Now observe the example of unlabeled form below. We have used two loops here two print '*'. In this example, if we haven't use break statement thus the loop will continue and it will give the output as shown below. 

class BreaklabDemo1
{
public static void main(String[] args)
{
  for (int i = 0; i < 10; i++) {
  System.out.print("\n");
  for (int j = 0; j<=i; j++)
 {
  System.out.print("*");
  if (j==5)
 // break;
 }
}
}

Output:

C:\javac>javac BreaklabDemo1.java

C:\javac>java BreaklabDemo1

*
**
***
****
*****
******
*******
********
*********
**********
C:\javac>

However in the following example we have used break statement. In this the inner for loop i.e. "for (int j=0; j<=i; j++)" will be executed first and gets terminated there n then. Then the outer for loop will be executed i.e. "for (int i=0; i<10; i++)". And it will give the output as shown below.

class BreaklabDemo{
  public static void main(String[] args){
  for (int i = 0; i < 10; i++) {
  System.out.print("\n ");
  for (int j = 0; j<=i; j++){
  System.out.print("*");
  if (j==5)
  break;
  }
  }
  }
}

Output:

C:\javac>javac BreaklabDemo.java

C:\javac>java BreaklabDemo

*
**
***
****
*****
******
******
******
******
******
C:\javac>

continue 

Continue statement is just similar to the break statement in the way that a break statement is used to pass program control immediately after the end of a loop and the continue statement is used to force program control back to the top of a loop. 
Lets see the same example of break statement but here we will use continue instead of break.

The continue statement skips the current iteration of a for, while , or do-while loop. The unlabeled form skips to the end of the innermost loop's body and evaluates the boolean expression that controls the loop.
For Instance,

class continueStatement 
{
public static void main(String[] args) 
{
   for (int i = 0; i < 5; i++) {
   System.out.println(i);
  if (i==3) {
   continue ;
   }
  }

 


C:\javac>javac continueStatement.java

C:\javac>java continueStatement
0
1
2
3
4

C:\javac>