Kamis, 24 April 2014

break continue and label in loop – Java program example




break and continue in Java are two essential keyword beginners needs to familiar while using loops ( for loop, while loop and do while loop). break statement in java is used to break the loop and transfers control to the line immediate outside of loop while continue is used to escape current execution and transfers control back to start of the loop. Both break and continue allows programmer to create sophisticated algorithm and looping constructs. In this java tutorial we will see example of break and continue statement in Java and some important points related to breaking the loop using label and break statement. break keyword can also be used inside switch statement to break current choice and if not used it can cause fall-through on switch. Well break is not alone on breaking switch case you can also sued throw keyword on switch case.





This Java tutorial is next in series of articles in looping like 4 ways to loop Map in Java and How to loop on ArrayList in Java. If you are new here and haven’t gone through those articles then you may find them useful.






break and continue statement in Java - example.



break and continue example in java  for loopIn this example we are calculating sum of odd numbers until number 5 appear, where we break the loop by using break statement. after calculating sum we also call continue which cause last line of loop to not execute. remember call to continue is inside if block which checks for odd numbers, so that line will appear in case of even number but not in case of odd number. This example of break and continue shows that upon calling break, loop terminates and control goes to first line outside the loop while upon calling continue, loop doesn't terminate but rest of execution doesn't happen and loop continues from next iteration.








/**

 * Simple Java program which demonstrate use of break and continue statements in Java

 * In this example break is used to break loop and continue is used to escape current  *iteration of loop once a condition is true.

 * @author

 */


public class BreakContinueExample {

 

    public static void main(String args[]) {

   

        int[] numbers= new int[]{1,2,3,4,5,6,7,8,9,10};

     

        //calculate sum of all numbers till 5 appears

        int sum = 0;

        for(int i=0; i< numbers.length; i++){

            System.out.println("Executing for loop with iteration number: " + i);

            if(i == 5){

                System.out.println("calling break statement to break for loop");

                break;

            }

            if(i%2 != 0){

                sum = sum + i;

                System.out.println("calling continue statement to start new iteration");

                continue;

            }

            System.out.println("Last line of loop, not executing for odd numbers due

                               to continue statement i: "
+ i);

        }

        System.out.println("Outside of for loop, sum: " + sum);

    }

}



Output:

Executing for loop with iteration number: 0

Last line of loop, not executing for odd numbers due to continue statement i: 0

Executing for loop with iteration number: 1

calling continue statement to start new iteration

Executing for loop with iteration number: 2

Last line of loop, not executing for odd numbers due to continue statement i: 2

Executing for loop with iteration number: 3

calling continue statement to start new iteration

Executing for loop with iteration number: 4

Last line of loop, not executing for odd numbers due to continue statement i: 4

Executing for loop with iteration number: 5

calling break statement to break for loop

Outside of for loop, sum: 4









continue and break examples with label in Java



You can use labels with break and continue. labels are where you can shift control from break and continue. by default break goes outside of loop but if you more than one loop you can use break statement to break a specific loop by providing label. same is true for continue. if you are working on nested loops labels gives more control to break and continue. In following example of break and continue with labels. we have a nested loop and we have created two labels OUTER and INNER. OUTER label is for outer loop and INNER label is for inner loop. we are iterating through array and print value of odd number from outer loop and then use continue statement, which ignores execution of inner loop. if its even number than inner loop executes and breaks as soon as it prints the number.






/**

 * Simple Java program which demonstrate use of break and continue statements in Java

 * with lables, break and continue can be used alongside label and loop.

 *

 * @author Javin

 */


public class BreakContinueWithLabel {

 

    public static void main(String args[]) {

   

        int[] numbers= new int[]{100,18,21,30};

     

        //Outer loop checks if number is multiple of 2

        OUTER:  //outer label

        for(int i = 0; i<numbers.length; i++){

            if(i % 2 == 0){

                System.out.println("Odd number: " + i + ", continue from OUTER label");

                continue OUTER;

            }

         

            INNER:

            for(int j = 0; j<numbers.length; j++){

                System.out.println("Even number: " + i + ", break  from INNER label");

                break INNER;

            }

        }

     

    }

}



Output:

Odd number: 0, continue from OUTER label

Even number: 1, break  from INNER label

Odd number: 2, continue from OUTER label

Even number: 3, break  from INNER label









As shown in above example of break and continue with labels, It provides lot of convenience to break and continue in case of nested loop.





important point about break and continue in Java





1. break keyword transfers control to next statement outside loop while continue keyword transfers control to beginning of loop, ignoring rest of lines in loop.





2. break can also be used inside CASE statement of switch construct.


3. an optional label can be provided after break and continue which cause to apply break and continue on specified loop.





That's all on break and continue statement in Java. break and continue allows programmer to control flow of execution in loops. We have also seen example of break and continue with and without labels , which allows you to write more sophisticated looping construct in java.








Other Java beginners tutorial you may like































Source:http://javarevisited.blogspot.com/2012/05/break-continue-and-lablel-in-loop-java.html

Tidak ada komentar:

Posting Komentar