break and continue

break:

break is used for exit from the loop based on some condition.

break can be used with labeled and unlabeled.

Example:

public class BreakLearn {
    private static final String[] ARR = {"India","Pakistan", "Austrelia", "Srilanka", 
            "South Africa"};
    public static void main(String arg[]){
        Search:
        for(int i = 0; i <= ARR.length; i++){
            System.out.println(ARR[i]);
            if("Austrelia".equals(ARR[i])){
                System.out.println("Entered into if condition");
                break Search;
            }
        }
    }
}

Continue:

Continue is used for stop the current execution and start the execution from the start of the loop or from the label specified .

Example:

private static final String[] ARR = { "India", "Pakistan", "Austrelia", "Srilanka", "South Africa" };

    public static void main(String arg[]) {
//        Search: 
            for (int i = 0; i < ARR.length; i++) {
            System.out.println(ARR[i]);
            if ("Austrelia".equals(ARR[i])) {
                System.out.println("Entered into if condition");
                continue;
            }
            System.out.println("Santhosh");
        }
    }

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s