Dark Mode On/Off

Java Break Statement Programs

In this tutorial, we will learn how to use the break statement in different scenarios. But before moving further, if you are not familiar with the concept of the break statement, then do check the article on Break Statement in Java.

Syntax:

``````jump-statement;
break;
``````

Program 1: Java Break Statement in a While loop

In this program, we will see how to use a break statement in a while loop while calculating the sum of all the positive numbers.

Algorithm:

1. Start
2. Declare variables for the number and sum.
3. Initialize the sum to 0.
4. Create an instance of the Scanner class.
5. Use a while loop to enter the numbers.
6. Check whether the number is positive or not.
7. Increment the sum in each iteration.
8. Break the loop if any negative number is entered.
9. Display the sum.
10. Stop.

Below is the code for the same.

``````//Java Program to see the implementation of break statement
import java.util.Scanner;

public class Main
{
public static void main(String[] args)
{
//Declare variables
int num, sum = 0;
//Take input from the user
// create an object of Scanner
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("Enter a number: ");
// initialize the elements
num = sc.nextInt();
// if number is negative then the loop terminates
if (num < 0)
{
break;
}
sum += num;
}
System.out.println("The sum of all positive numbers is = " + sum);
}
}``````

Enter a number: 2
Enter a number: 3
Enter a number: 5
Enter a number: 4
Enter a number: -9
The sum of all positive numbers is = 14

Program 2: Java Break Statement in a do-while loop

In this program, we will see how to use a break statement in a do-while loop while calculating the sum of all the positive numbers.

Algorithm:

1. Start
2. Declare variables for the number and sum.
3. Initialize the sum to 0.
4. Create an instance of the Scanner class.
5. Use a do-while loop to enter the numbers.
6. Check whether the number is positive or not.
7. Increment the sum in each iteration.
8. Break the loop if any negative number is entered.
9. Display the sum.
10. Stop.

Below is the code for the same.

``````//Java Program to see the implementation of break statement
import java.util.Scanner;

public class Main
{
public static void main(String[] args)
{
//Declare variables
int num, sum = 0;
//Take input from the user
// create an object of Scanner
Scanner sc = new Scanner(System.in);

do
{
System.out.println("Enter a number: ");
// initialize the elements
num = sc.nextInt();
// if number is negative then the loop terminates
if (num < 0)
{
break;
}
sum += num;
}while (true);
System.out.println("The sum of all positive numbers is = " + sum);
}
}``````

Enter a number: 6
Enter a number: 7
Enter a number: 8
Enter a number: 5
Enter a number: 4
Enter a number: -2
The sum of all positive numbers is = 30

Program 3: Java Break Statement in a For Loop

In this program, we will see how to use a break statement in a for loop.

Algorithm:

1. Start
2. Use a for loop that iterates from 1 to 10.
3. Break the loop if 6 is encountered.
4. Print all the elements before the break statement executes.
5. Stop.

Below is the code for the same.

``````//Java Program to see the implementation of break statement
import java.util.Scanner;

public class Main
{
public static void main(String[] args)
{
System.out.println("The elements are: ");
for(int i=1;i<10;i++)
{
if(i==6)
{
break;
}
System.out.println(i);
}
}
}``````

The elements are:
1
2
3
4
5

Want to learn coding?
Try our new interactive courses.
Over 20,000+ students enrolled.