knowledgediarybd.com

Your Virtual Knowledge Campus







Break and Continue Statements in java

Break and Continue StatementBreak and Continue Statements  are used  in java for controlling  various kind of loop such as for loop, while loop & do..while loop etc.

 

Explanation of break and continue statements are given below

1.The break Keyword or Statement:

The break keyword is used to stop the entire loop. The break keyword must be used inside any loop or a switch statement.

The break keyword will stop the execution of the innermost loop and start executing the next line of code after the block.

Syntax:

break;

For More details see the following figure:

break

Example of break Statement:

public class Kabir {

public static void main(String args[]) {

int [] numbers = {10, 20, 30, 40, 50};

for(int x : numbers ) {

if( x == 30 ) {

break;

}

System.out.print( x );

System.out.print("\n");

}

}

}

This would produce following result:

10

20

2.The continue Keyword:

The continue keyword can be used in any of the loop control structures. It causes the loop to immediately jump to the next iteration of the loop.

  1. In a for loop, the continue keyword causes flow of control to immediately jump to the update statement.
  2. In a while loop or do/while loop, flow of control immediately jumps to the Boolean expression.

Syntax of Continue Statement:

continue;

For More details see the following figure:

continue

Example:

public class Kabir {

public static void main(String args[]) {

int [] numbers = {10, 20, 30, 40, 50};

for(int x : numbers ) {

if( x == 30 ) {

continue;

}

System.out.print( x );

System.out.print("\n");

}

}

}

The output of the above code looks like below:

10

20

40

50

[Note:] More interested things are waiting to publish..So Stay connected to us..

——————-:Thank you:—————–

Related Post or you may Like:

knowledgediarybd.com © 2017
shares