break in java

Previous
Next

Break:

  • A branching statement used to terminate the execution of a loop or switch.
  • We cannot break a block because block executes only once.
  • We can break either loop or switch statement.

We can break infinite loops also:

class Loops
{
	public static void main(String[] args) 
	{
		while(true)
		{
			System.out.println("Infinite loop can break");
			break;
		}
	}
}
  • We cannot break the block – Block executes only once and terminates automatically
  • We cannot use break outside to switch or loop
class Test 
{
	public static void main(String[] args) 
	{
		if(true)
		{
			System.out.println("Block executs only once - hence no need break");
			break;   // Error : 
		}
	}
}

We can break the loop on condition with if block:

class Loops
{
	public static void main(String[] args) 
	{
		for(int i=1 ; i<=10 ; i++)
		{
			if(i==5)
			{
				break;
			}
			System.out.println("i val : " + i);
		}
	}
}

Nested while loop – Infinite execution:

class Test 
{
	public static void main(String[] args) 
	{
		while(true)
		{
			System.out.println("Outer stats");
			while(true)
			{
				System.out.println("Inner stats");
			}
		}
	}
}

Output:
Outer stats
Inner stats
Inner stats
Inner stats
.....

Break statement can terminate only one loop which is surrounded by break:

class Test 
{
	public static void main(String[] args) 
	{
		while(true)
		{
			System.out.println("Outer stats");
			while(true)
			{
				System.out.println("Inner stats");
				break;
			}
		}
	}
}

Output:
Outer stats
Inner stats
Outer stas
Inner stats
.......

Two break statements required to break inner loop and Outer loop:

class Test 
{
	public static void main(String[] args) 
	{
		while(true)
		{
			System.out.println("Outer stats");
			while(true)
			{
				System.out.println("Inner stats");
				break;
			}
			break;
		}
	}
}

Output:
Outer stats
Inner stats

Read and Check the input number is even or not:

import java.util.Scanner ;
class Test 
{
	public static void main(String[] args) 
	{
		Scanner scan = new Scanner(System.in);
		System.out.print("Enter an integer : ");
		int n = scan.nextInt();
		if(n%2==0)
			System.out.println(n + " is even");
		else
			System.out.println(n + " is not even");
	}
}

We use do while loop to check the number is even or not continuously:

import java.util.Scanner ;
class Test 
{
	public static void main(String[] args) 
	{
		Scanner scan = new Scanner(System.in);
		System.out.print("Enter an integer : ");
		int n = scan.nextInt();
		do
		{
			if(n%2==0)
				System.out.println(n + " is even");
			else
				System.out.println(n + " is not even");

			System.out.println("Do you want to check another number (0 to stop) : ");
			n = scan.nextInt();
			if(n==0)
				break;
		}
		while (true);
	}
}

Break statement inside inner for loop:

class Break
{
	public static void main(String[] args) 
	{
		for (int i=1 ; i<=5 ; i++)
		{
			for(int j=1 ; j<=5 ; j++)
			{
				if(i==j)
					break ; 
				System.out.println("i : "+i+"\t j : "+j);
			}
		}
	}
}
Previous
Next

Add Comment

Courses Enquiry Form