Hero Background

Next-Gen App & Browser Testing Cloud

Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles

Next-Gen App & Browser Testing Cloud

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

Test Your Website on 3000+ Browsers

Get 100 minutes of automation test minutes FREE!!

Test Now...

KaneAI - Testing Assistant

World’s first AI-Native E2E testing agent.

...

TestMu AI forEnterprise

Get access to solutions built on Enterprise
grade security, privacy, & compliance

  • Advanced access controls
  • Advanced data retention rules
  • Advanced Local Testing
  • Premium Support options
  • Early access to beta features
  • Private Slack Channel
  • Unlimited Manual Accessibility DevTools Tests