0% found this document useful (0 votes)
250 views8 pages

Break and Continue Statement in Java

Break and Continue Statement in Java

Uploaded by

BlackMetal101
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
250 views8 pages

Break and Continue Statement in Java

Break and Continue Statement in Java

Uploaded by

BlackMetal101
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 8

Break and Continue Statement in Java With

Examples
In this tutorial, you will learn about the Break and Continue statement in
Java with
the help of examples.

Java has some jump statements, which shift the program flow to another part of
the program.
Normally, after the body of a loop has been entered, all the
statements in the body are executed before doing the
next loop test.
The break
and continue statements are used to skip part of a loop or even terminate it
before the
predetermined condition, depending on tests made on the body of the
loop.
It can be also be used to handle errors or some exceptional conditions.

1. Break Statement :
When we use the break statement, the program control resumes at the next
statement following the loop. If the
break statement is inside nested loops, it
brings the control out of the loop containing it.

Syntax :

break;

Flow chart for Break Statement


Example for Break Statement

// Java Program to

import java.io.*;

public class pattern {

public static void main(String[] args) {

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

if (i == 8) {
break;

}
System.out.println("Number is : " + i);

Output :

Number is : 0

Number is : 1

Number is : 2

Number is : 3

Number is : 4

Number is : 5

Number is : 6

Number is : 7

In the above example, the loop is terminated when the value of i is 8. Otherwise,
Its value is further processed
in the
loop.

Labeled Break Statement :


We can also use the break statement with a label.
Control can be transferred from the inner loop to the outer loop of the
program by using the label.
when we want the flow of control to exit the outer loop then we use a
labeled break statement.
Syntax :

break label;

Example for Labeled Break Statement.

// Java program to demonstrate labeled break statement.

import java.io.*;

public class pattern {

public static void main(String[] args) {

a: for (int i = 1; i <= 4; i++) {

b: for (int j = 1; j <= 2; j++) {

if (i == 3) {

// break statement with label

break a;

System.out.println(i + " " + j);

}
}

Output :

1 1

1 2

2 1

2 2

2. Continue Statement :
Continue statement is a branching statement. It is used to continue the loop.
When encountered, it causes the rest of the iteration to be skipped and the next
iteration starts.
The continue statement is almost always used in decision-making
statements.
We can use Java continue statement in all types of loops such as for
loop, while loop, and do-while loop.

Syntax :
continue;

Flow chart for continue statement :

Example for Continue Statement in Java.

// Java Program to demonstrate the continue statement in for loop.

import java.io.*;

public class pattern {

public static void main(String[] args) {

int i;

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

if (i == 3) {

continue;

}
System.out.println(i);

Output :

The value 4 is missing in the output, because when the value of variable i is 3,
then the program executes a
continue statement, which makes it jump at the
beginning of for loop for the next iteration, skipping the
statements for the
current iteration.

Example for Continue Statement with Inner Loop

import java.util.Scanner;

public class pattern {

public static void main(String[] args) {

int i = 1, j = 1;

while (i <= 5) // outer while loop

System.out.println("Outer Loop: " + i);

while (j <= 5) //inner while loop

{
if (j == 3) {

j++;

continue;

System.out.println("Inner Loop: " + j);

j++;

}
i++;

Output :

Outer_Loop: 1

Inner_Loop: 1

Inner_Loop: 2

Inner_Loop: 4

Inner_Loop: 5

Outer_Loop: 2

Outer_Loop: 3

Outer_Loop: 4

Outer_Loop: 5
Here, the First Outer Loop condition will be checked, if " i " is greater than or
equal to 5 then the loop
will terminate and
come out of the loop.
If the value of " i " is less than equal to 5 then control goes inside the loop
and print the " i " value
after
that check for the condition in 2nd while loop
if it is true then goes inside the loop and execute statements
otherwise come
out of the loop and continue until 1st while loop becomes false.
In the 2nd while loop we used the "if" condition inside that we used the
continue statement. In case of an
inner loop, it
continues the inner loop only
and then outer loop. So when the value of "j == 3" then the value of "j" is
increased and
the continue statement is executed and skips the iteration of
the inner loop. Hence, the text Inner_Loop: 3 is
skipped from the output.

Labeled Continue Statement


This is another form of continue statement in Java known as labeled
continue.
It includes the label of the loop along with the continue keyword.
The continue statement skips the current iteration of the loop specified by
the label.
Syntax :

continue label;

Example for Labeled Continue Statement

// Java program to perform Labeled continue Statement.

import java.util.Scanner;

public class pattern {

public static void main(String[] args) {

hashcode: for (int i = 1; i <= 4; i++) {

// inner loop

for (int j = 1; j <= 3; j++) {

if (i == 2 && j == 2) {

//using continue statement with label

continue hashcode;

System.out.println("i = " + i + " j = " + j);

}
}

Output :

i = 1 j = 1

i = 1 j = 2

i = 1 j = 3

i = 2 j = 1

i = 3 j = 1

i = 3 j = 2

i = 3 j = 3

i = 4 j = 1

i = 4 j = 2

i = 4 j = 3

Previous Tutorial:
Loops in Java

Next Tutorial: Login


Arrays in Java

Subscribe to HashCodec for new updates

Enter Email Address*

Subscribe
Email : support@hashcodec.com

Tutorials
C Programming
Java Programming

Python Programming

Javascript Programming

Company
About Us

Privacy & Policy

Terms & Conditions

FAQ

Our Social Networks


Join us in the social networks to receive the latest news and updates.

© 2021 HashCodec. All Rights Reserved.

You might also like