12-break,continue,return keyword in java

  • break-break keyword caused the loop to break the execute immediately.

public class Testing {
    public static void main(String args[]){

        int x=0;
        for(int i=0;i<10;i++){
            System.out.println("point 1");
            x++;

            if(x==5){ //there should be condition before using break
                break;
            }
            System.out.println("point 2");
        }
    }
}

  • continue-continue keyword caused the loop to go to the next iteration immediately.

public class Testing {
    public static void main(String args[]){

        int x=0;
        for(int i=0;i<10;i++){
            System.out.println("point 1");
            x++;

            if(x==5){//there should be condition before using break
                continue;
            }
            System.out.println("point 2");
        }
    }
}

  • return- return keyword caused the thread to immediately come out of the memory.

public class Testing {
    public static void main(String args[]){

        int x=0;
        for(int i=0;i<10;i++){
            System.out.println("point 1");
            x++;

            if(x==5){//there should be condition before using break
                return;
            }
            System.out.println("point 2");
        }
        System.out.println("point 3"); // this will not printed on screen.
    }
}

Leave a comment