Continuous Test Orchestration And Execution Platform Online

Perform automated and live-interactive testing on 3000+ real desktop and mobile devices online.

Write a Java program to demonstrate the usage of break and continue statements inside a While loop?

In Java, the break and continue are jump statements that ignore specific statements within the loop or abruptly terminate the loop without executing the test expression. These statements can be used within any loop, including for, while, and do-while loops.

Following is the Java program that demonstrates the use of break statements inside a While loop.

Code:


public class Example {  
public static void main(String[] args) {  
    //initiating while loop  
    int a=1;  
    while(a<=10){  
        if(a==5){  
            //using break statement  
            a++;  
            break;//it will break the while loop  
        }  
        System.out.println(a);  
        a++;  
    }  
}  
}  

Output:

1

2

3

4

5

Following is the Java program that demonstrates the use of continue statement inside a While loop.

Code:


public class Example {  
public static void main(String[] args) {  
    //initiating while loop  
    int a=1;  
    while(a<=10){  
        if(a==5){  
            //using continue statement  
            a++;  
            continue;//it will skip the remaining statement  
        }  
        System.out.println(a);  
        a++;  
    }  
}  
}  

Output:

1

2

3

4

5

6

7

8

9

10

LambdaTest

Test your websites, web-apps, or mobile apps seamlessly with LambdaTest.

Start Free Testing
LambdaTest

Earn resume-worthy Selenium certifications that help you land a top job.

Learn More
LambdaTest

Test your web or mobile apps

Test orchestration and execution cloud of 3000+ browsers and OS

Support

24/7 support

Security

Enterprise grade security

Cloud

Fastest test execution cloud