LogIn
I don't have account.

Break statement in java

DevSniper
140 Views

Break statement is a fundamental control flow statement of Java. break statement is used to terminates the closest enclosing loop or switch statement. It is useful where we want to exit the closest enclosing loop or switch after meeting certain condition and resume execution at the statement immediately following the terminated loop or switch.

  • break statement is used to terminates the closest enclosing loop or switch statement.
  • In case of nested loop it break or terminate only the innermost loop that contains it
  • break enhances code readability by clearly defining exit points from loops and switch statements.
  • We should always include break statements in each case of a switch statement to prevent unintended fall-through behavior.
  • It makes code easier to understand ,readable and maintain, especially in complex logic structures.
  • Break statement with loop
  • Break statement with switch
  • Break statement with nested loop
  • Break statement with loop and switch

Break statement with loop

Break statement with for loop

public class RebootMemory 
{
    public static void main(String[] args) 
    {
        for(int i=1; i<5; i++) {
            System.out.println("brfoer break i : "+i);
            if(i==2) {
                System.out.println("BREAKING FOR LOOP FOR i : "+i);
                break;
            }
            System.out.println("after break i : "+i);
        }
    }
}
brfoer break i : 1
after break i : 1
brfoer break i : 2
BREAKING FOR LOOP FOR i : 2

Break statement with while loop

public class RebootMemory 
{
    public static void main(String[] args) 
    {
        int i=1;
        while(i<5) {
            System.out.println("brfoer break i : "+i);
            if(i==2) {
                System.out.println("BREAKING while LOOP FOR i : "+i);
                break;
            }
            System.out.println("after break i : "+i);
            i++;
        }
    }
}
brfoer break i : 1
after break i : 1
brfoer break i : 2
BREAKING while LOOP FOR i : 2

Break statement with do while loop

public class RebootMemory 
{
    public static void main(String[] args) 
    {
        int i=1;
        do {
            System.out.println("brfoer break i : "+i);
            if(i==2) {
                System.out.println("BREAKING do while LOOP FOR i : "+i);
                break;
            }
            System.out.println("after break i : "+i);
            i++;
        }while(i<5);
    }
}
brfoer break i : 1
after break i : 1
brfoer break i : 2
BREAKING do while LOOP FOR i : 2

Break statement with switch

public class RebootMemory 
{
    public static void main(String[] args) 
    {
        int n=10;
        switch(n%3) {
        case  0:
            System.out.println(n + " is divided by 3");
            break;
        case 1:
            System.out.println(n+ " is not divided by 3 , 1 is the remainder");
            break;
        case 2:
            System.out.println(n +" is not divided by 3 , 2 is the remainder");
            break;
        default :
            System.out.println("default case , n " +n);
            break;
        }
    }
}
10 is not divided by 3 , 1 is the remainder

Break statement with nested loop

if we place one loop inside another loop this is called nested loop. inner loop execute completely for every outer loop execution. we can place break statement inside inner loop that will break only inner loop and out loop will continue execution. and if we place break statement inside outer loop and outside inner loop this will break/exit outer loop execution due to this inner and outer both loop will stop execution.

Break statement inside inner loop

public class RebootMemory 
{
    public static void main(String[] args) 
    {
        for(int i=1; i<4; i++) {
            System.out.println("outer loop i : "+i);
            for(int j=0; j<4; j++) {
                System.out.println("\tinner loop j : "+j);
                if(i==2) {
                    System.out.println("BREAKING INNER LOOP FOR i : "+i);
                    break;
                }
            }
            System.out.println("outer loop after inner loop i : "+i);
        }
    }
}
outer loop i : 1
        inner loop j : 0
	inner loop j : 1
	inner loop j : 2
        inner loop j : 3
outer loop after inner loop i : 1
outer loop i : 2
        inner loop j : 0
BREAKING INNER LOOP FOR i : 2
outer loop after inner loop i : 2
outer loop i : 3
        inner loop j : 0
	inner loop j : 1
        inner loop j : 2
        inner loop j : 3
outer loop after inner loop i : 3

Break statement inside outer loop and outside inner loop

public class RebootMemory 
{
    public static void main(String[] args) 
    {
      for(int i=1; i<4; i++) {
            System.out.println("outer loop i : "+i);
            if(i==2) {
                System.out.println("BREAKING OUTER LOOP FOR i : "+i);
                break;
            }
            for(int j=0; j<4; j++) {
                System.out.println("\tinner loop j : "+j);
            }
            System.out.println("outer loop after inner loop i : "+i);
        }
    }
}
outer loop i : 1
        inner loop j : 0
	inner loop j : 1
	inner loop j : 2
	inner loop j : 3
outer loop after inner loop i : 1
outer loop i : 2
BREAKING OUTER LOOP FOR i : 2

Break statement with loop and switch

public class RebootMemory 
{
    public static void main(String[] args) 
    {
        int[] arr= {11,12,13,14,15,16};
        for( int n : arr) {
            switch(n%3) {
            case  0:
                System.out.println(n + " is divided by 3");
                break;
            case 1:
                System.out.println(n+ " is not divided by 3 , 1 is the remainder");
                break;
            case 2:
                System.out.println(n +" is not divided by 3 , 2 is the remainder");
                break;
            default :
                System.out.println("default case , n " +n);
                break;
            }
        }
    }
}
11 is not divided by 3 , 2 is the remainder
12 is divided by 3
13 is not divided by 3 , 1 is the remainder
14 is not divided by 3 , 2 is the remainder
15 is divided by 3
16 is not divided by 3 , 1 is the remainder