Loop Control Statements: Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed.

Java supports the following control statements.

  • Break Statement
  • Continue Statement

Break Statement:

The break statement in Java programming language has the following
two usages −

  • When the break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop.
  • It can be used to terminate a case in the switch statement.

Syntax:
The syntax of a break is a single statement inside any loop:
break;

Flow Diagram:

Break Statement

Example:

public class Test {
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");
}
}
}

In the above program we use the break statement and this program will produce the following result:
Output

10
20

Continue Statement:

The continue keyword can be used in any of the loop control structures or any loop control statement. It causes the loop to immediately jump to the next iteration of the loop. In a for loop, the continue keyword causes control to immediately jump to the update statement. In a while loop or do/while loop, control immediately jumps to the Boolean expression.

Syntax:
The syntax of a continue is a single statement inside any loop −
continue;

Flow Diagram:

Continue statement

Example:

public class Test {
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");
}
}
}

This will produce the following result :
Output:

10
20
40
50

You want to know about what is loop? and loop control statements in java so please see our article named Loops and Loops Control Statements in Java.

Leave a Reply

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

Back to Top