Video 24(ch5)

Yash
0

 



Program:-

Break statement:-


public class BreakStatementV24 {

    public static void main(String []args){

        System.out.println("break in for loop:");

        for(int i =0;i<=5;i++){

            System.out.print(i+".");

            System.out.println("java is great");

            if(i==2){

                System.out.println("Ending of the loop");

                break; /*break statement is used to exit the loop irrespective of weather the condition is true

                false here first the 'java is great' print three time and the the loop is exit when the value of

                'i' becomes 2*/

            }

        }

        System.out.println("Loop ends here\n");

        //we can also use the break statement in while and do-while loop.

        System.out.println("break in while loop:");

        int j = 0;

        while(j<=5){

            System.out.print(j+".");

            System.out.println("java is great");

            if(j==2){

                System.out.println("Ending of the loop");

                break;

            }

            j++;

        }

        System.out.println("Loop ends here\n");

        System.out.println("break in do-while loop:");

        int k = 0;

        do{

            System.out.print(k+".");

            System.out.println("java is great");

            if(k==2){

                System.out.println("Ending of the loop");

                break;

            }

            k++;

        }while(k<=5);

        System.out.println("Loop ends here");

    }

}


Continue statement:-

public class ContinueStatementV24 {
    public static void main(String []args){
    System.out.println("continue in for loop:");
    for(int i =0;i<=5;i++){
        if(i==2){
            System.out.println("Ending of the loop");
            continue; /* The continue statement is used to immediately move to the next iteration of the loop
            here in tis loop the loop will continue till the i becomes equal to 2 and after that the when continue
            statement is called the syntax below the continue statement present in loop will not execute for i=2
            and go on the next iteration i.e i= 3*/
        }
        System.out.print(i+".");
        System.out.println("java is great");
    }
    System.out.println("Loop ends here\n");
    //we can also use the break statement in while and do-while loop.
    System.out.println("continue in while loop:");
    int j = 0;
    while(j<5){
        j++;
        if(j==2){
            System.out.println("Ending of the loop");
            continue;
        }
        System.out.print(j+".");
        System.out.println("java is great");
    }
    System.out.println("Loop ends here\n");
    System.out.println("continue in do-while loop:");
    int k = 0;
    do{
        k++;
        if(k==2){
            System.out.println("Ending of the loop");
            continue;
        }
        System.out.print(k+".");
        System.out.println("java is great");
    }while(k<5);
    System.out.println("Loop ends here");
}
}

Tags

Post a Comment

0 Comments
Post a Comment (0)
To Top