PUBLISHED ON: APRIL 22, 2021
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:
- Start
- Declare variables for the number and sum.
- Initialize the sum to 0.
- Create an instance of the Scanner class.
- Use a while loop to enter the numbers.
- Check whether the number is positive or not.
- Increment the sum in each iteration.
- Break the loop if any negative number is entered.
- Display the sum.
- 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:
- Start
- Declare variables for the number and sum.
- Initialize the sum to 0.
- Create an instance of the Scanner class.
- Use a do-while loop to enter the numbers.
- Check whether the number is positive or not.
- Increment the sum in each iteration.
- Break the loop if any negative number is entered.
- Display the sum.
- 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:
- Start
- Use a for loop that iterates from 1 to 10.
- Break the loop if 6 is encountered.
- Print all the elements before the break statement executes.
- 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