
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference Between Continue and Break Statements in Java
As we know in programming execution of code is done line by line. Now in order to alter this flow Java provides two statements break and continue which are mainly used to skip some specific code at specific lines.
Difference Table
Following are the important differences between continue and break ?
Sr. No. | Key | Break | Continue |
---|---|---|---|
1 | Functionality | Break statements are mainly used to terminate the enclosing loop such as while, do-while, for, or switch statements wherever a break is declared. | The continue statement mainly skips the rest of the loop wherever the continue is declared and executes the next iteration. |
2 | Execution flow | The break statement resumes the control of the program to the end of the loop and makes execution flow outside that loop. | The continue statement resumes the control of the program to the next iteration of that loop enclosing 'continue' and making execution flow inside the loop again. |
3 | Usage | As mentioned break is used for the termination of enclosing loop. | On the other hand, continuing causes early execution of the next iteration of the enclosing loop. |
4 | Compatibility | The break statement can be used and compatible with 'switch', and 'label'. | We can't use the continue statements with 'switch', and 'lablel' as it is not compatible with them. |
Example of Continue vs Break
The below example demonstrates the difference between continue vs break ?
Example
public class JavaTester{ public static void main(String args[]){ // Illustrating break statement (execution stops when value of i becomes to 4.) System.out.println("Break Statement\n"); for(int i=1;i<=5;i++){ if(i==4) break; System.out.println(i); } // Illustrating continue statement (execution skipped when value of i becomes to 1.) System.out.println("Continue Statement\n"); for(int i=1;i<=5;i++){ if(i==1) continue; System.out.println(i); } } }
Output
Break Statement 1 2 3 Continue Statement 2 3 4 5
Advertisements