DEV Community

Nanthini Ammu
Nanthini Ammu

Posted on

break,return, and continue in java

break:

It is used to stop the loop or exit the switch completely.

continue:

It is used to skips the current ittertaion and moves to the next iteration.

return:

It is used to stop the entire method, not just the loop.

return Example :

return; // void method
return value; // method with return type

public class ReturnDemo {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int val = sc.nextInt();
        if(val<0){
            System.out.println("invalide age");
            return;
        }

        System.out.println("Age is "+val);
        int marks = sc.nextInt();
        ReturnDemo obj = new ReturnDemo();
        String grade = obj.getGrade(marks);
        System.out.println("Grade is "+grade);

    }

    String getGrade(int a)
    {
        if(a >= 90) return "A";
        if(a >= 80) return "B";
        if(a >= 70) return "C";
        if(a >= 50) return "D";
        return "F";
    }
}
Enter fullscreen mode Exit fullscreen mode

break Example :

break; // exits current loop or switch
break label; // exits labeled loop (nested loops)

public class BreakDemo {

    public static void main(String[] args) {

        // break in loop

        for (int i = 1; i <= 5; i++) {
            if (i == 5)
                break;
            System.out.println(i);
        }

        System.out.println("Loop ended when the value is 5");
        System.out.println("****************************************");

        //break in switch

        int day = 3;
        switch (day) {
            case 1:
                System.out.println("Monday");
                break;          // exits switch
            case 2:
                System.out.println("Tuesday");
                break;
            case 3:
                System.out.println("Wednesday");
                break;          // exits switch
            default:
                System.out.println("Other day");

        }
        System.out.println("****************************************");

        //break in nested loops
        for(int i =1; i<=5; i++){
            for(int j = 1; j<=5; j++){
                if(j ==2 )
                    break;
                System.out.println(i+" "+j);
            }
        }
        System.out.println("****************************************");
        //break outer loop - labeled break
        outer:
        for(int i =1; i<=5; i++){
            for(int j = 1; j<=5; j++){
                if(i ==2 )
                    break outer;
                System.out.println(i+" "+j);
            }
        }
    }
}

Output :
1
2
3
4
Loop ended when the value is 5
****************************************
Wednesday
****************************************
1 1
2 1
3 1
4 1
5 1
****************************************
1 1
1 2
1 3
1 4
1 5

Enter fullscreen mode Exit fullscreen mode

continue example:

continue; // skips current iteration
continue label; // skips to labeled loop's next iteration

public class ContinueDemo {

    public static void main(String[] args) {

        //continue in for loop
        for(int i=0; i<=10;i++)
        {
            if(i%2 ==0)
                continue;
            System.out.println(i);
        }

        System.out.println("*********************************");

        //continue in nested loops

        for (int i = 1; i <= 3; i++) {
            for (int j = 1; j <= 3; j++) {
                if (j == 2) {
                    continue;   // skips j=2 in inner loop
                }
                System.out.println(i + " " + j);
            }
        }

        System.out.println("*********************************");

        //labeled continue
        outer:
        for(int i=1; i<=3;i++){
            for(int j=1; j<=3;j++){
                if(j == 2){
                    continue outer;
                }
                System.out.println(i+" "+j);
            }
        }
    }
}

Output:
1
3
5
7
9
*********************************
1 1
1 3
2 1
2 3
3 1
3 3
*********************************
1 1
2 1
3 1
Enter fullscreen mode Exit fullscreen mode

Top comments (0)