Updated March 18, 2023
Difference Between Break and Continue
In any programming language, there are some important keywords, and every keyword has its own meaning. Almost in all the programming languages like C, C++, Java, Python, etc., used for web development, there are 2 keywords that are common and frequently used, i.e. ‘break’ and ‘continue’. Break vs Continue though being keywords are the jump statements and are used in the case of loops and switch cases to perform actions according to the programmer’s requirements. There were some situations when we wanted some modifications in the execution of loop or block statements when certain criteria met.
Consider two scenarios, scenario 1 of searching an employee by id in an array and scenario 2 of printing the names of all employees except for one employee from the array.
For the former, ‘break’ statement is used as once the required employee is found, we want to exit the loop and does not want any loop execution further, and for the later ‘continue’ statement will be used as we need to skip the printing of a particular employee and once that employee matches, it will skip it and move the control 1 iteration forward and continues its execution. A break is used to exit the loop when a certain condition is met, whereas Continue is used to continue in C# skip the current iteration and resumes to the next iteration of the loop.
Head to Head Comparison Between Break and Continue(Infographics)
Below are the top 5 differences between Break vs Continue:
Key Differences Between Break and Continue
Let us discuss some of the major key differences between Break vs Continue:
- A break is used to abruptly terminate the execution of the upcoming statements and iterations of a loop and move to the next statement after the loop, whereas continue is used for a different purpose, i.e. to skip the current iteration and move to the next iteration.
- The break statement allows the control to move out of the loop, skipping the execution of the remaining statements of the loop whenever encountered, whereas they continue to allow the control to remain inside the loop only by moving 1 iteration ahead.
- When talking about the cause of both the jump statements, the break statement causes the termination or exit from the loop, whereas the continue statement allows for the early/ quick execution of the loop.
- One of the most important things that need to be kept in mind regarding the use of break and continue statements are that break statement can be used with the loops (for, while, for each, do.. while, etc.) as well as with the switch and labels whereas continue statement can be used only with loops (for, while, for each, etc.) and can never be used with the switch and labels.
- As the break keyword allows us to move out of the loop, we can say it does not allow the continuation of the loop, whereas the continue keyword allows the continuation of the same loop.
- A break statement inside the nested loop allows the termination of the innermost loop, and the control remains inside the outermost loop (it will not affect the outermost loop), whereas continuing statement inside the nested loop allows the skip of the current iteration and execution of the next iteration of the innermost loop.
Example of the break statement:
Code
public class breakCheck{
public static void main(String[] args)
{
for (int i=0; i<5; i++)
{
for (int j=1;j<3;j++)
{
System.out.println("hello the value of i is: "+ i);
if(j==1)
break;
}
}
}
}
Output:
In the above example, once the value of the inner loop variable, ‘j’ becomes 1, the desired condition will be met and be encountering the ‘break’ keyword, then the innermost loop will terminate, but the control remains inside the outermost loop, and it will work as expected and keep on incrementing normally.
Example of continue statement:
Code
public class continueCheck{
public static void main(String[] args)
{
for (int i=0; i<5; i++)
{
if(i==3)
continue;
System.out.println("hello the value of i is: "+ i);
}
}
}
Output:
In the above example, when the value of the variable ‘i’ is 0,1,2, there is no issue, and the control is working as expected. Once the value of the variable ‘i’ becomes 3, it meets the desired condition. The continue keyword keeps the execution of the current statement and moves towards the next iteration.
Break vs Continue Comparison Table
Let’s discuss the top comparison between Break vs Continue:
S.No. | Break | Continue |
|
A break is basically used to terminate the execution of a loop or a switch statement. Whenever a break statement is encountered, execution of that loop or switch statement ends abruptly. | The continue statement is used for the termination of the current iteration and not the whole loop. It allows the control to remain inside the loop, skip the current iteration and move to the next iteration. |
2. | Upcoming statements or leftover iterations are not executed after the break statement is encountered in a loop (for, while, for each, do .. while) | Leftover Iterations are executed even if the continue keyword is encountered in a loop (for, while, for each, do .. while) |
3. | The breaks can be used with other block statements apart from the loops, such as switch and label statements. | Continue can never be used with the switch and label statements and is used only with the loops. |
4. | The breaks statement is used to break the iteration or the discontinuation of the whole loop. | Continue statements are used only to skip the iteration and move to the next iteration of the loop. |
5. | Break statement in any loop, switch, and label do not resume the execution of iterations once encountered. | Continue statement in any loop resumes the control to the next iteration once encountered. |
Conclusion
The above explanation clearly describes the difference between the two jump statements, i.e. break vs continue. Though they are quite different and used for different purposes, but for freshers, it is tricky to understand them, especially when it comes to nesting loops. As they are commonly used statements in any programming language, it is important to understand them thoroughly before using them according to any specific situation.
Recommended Articles
This is a guide to Break vs Continue. Here we discuss the difference between Break vs Continue, key differences with infographics, and comparison table. You can also go through our other suggested articles to learn more–