The Wayback Machine - https://web.archive.org/web/20240710163445/https://www.geeksforgeeks.org/break-vs-continue-statement-in-programming/
Open In App

Break vs Continue Statement in Programming

Last Updated : 24 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Break and Continue statements are the keywords that are used always within a loop. The purpose of a break and continue statement is to stop a running loop or to continue a particular iteration. In this article we will see what are the break and continue statements, what is their use and what are the differences between them.

Break vs Continue Statement

Break vs Continue Statement

Break Statement:

A break statement is used when we want to terminate the running loop whenever any particular condition occurs. Whenever a break statement occurs loop breaks and stops executing.

For example: If we have a variable curr=0 and in a while loop we are incrementing it by one each time and printing it. Now we want to stop whenever we get curr=5. So we can write a break statement when we get curr=5.

Below is the implementation of the above idea:

C++
#include <iostream>
using namespace std;

int main()
{
    int curr = 0;
      // Loop till curr < 10
    while (curr < 10) {
        cout << curr << endl;
        curr++;
          // If curr = 5, break out of the loop
        if (curr == 5) {
            break;
        }
    }
    return 0;
}
Java
public class Main {
    public static void main(String[] args)
    {
        int curr = 0;
        // Loop till curr < 10
        while (curr < 10) {
            System.out.println(curr);
            curr++;
            // If curr = 5, break out of the loop
            if (curr == 5) {
                break;
            }
        }
    }
}
Python
curr = 0
# Loop till curr < 10
while curr < 10:
    print(curr)
    curr += 1
    # If curr = 5, break out of the loop
    if curr == 5:
        break
JavaScript
let curr = 0;
// Loop till curr < 10
while (curr < 10) {
    console.log(curr);
    curr++;
    // If curr = 5, break out of the loop
    if (curr === 5) {
        break;
    }
}

Output
0
1
2
3
4

Continue Statement:

On the other side continue statement is used when we have to skip a particular iteration. Whenever we write continue statement the whole code after that statement is skipped and loop will go for next iteration.

For example: We have to print numbers form 1 to 5 but skip 3 . So we can use a for loop which will print the number and when i=3 we will continue.

Below is the implementation of the above idea:

C++
#include <iostream>
using namespace std;

int main()
{
    // Loop till i <= 5
    for (int i = 0; i <= 5; ++i) {
        // If i = 3, then continue to the next iteration
        if (i == 3) {
            continue;
        }
        cout << i << endl;
    }
    return 0;
}
Java
public class Main {
    public static void main(String[] args)
    {
        // Loop till i <= 5
        for (int i = 0; i <= 5; ++i) {
            // If i = 3, then continue to the next iteration
            if (i == 3) {
                continue;
            }
            System.out.println(i);
        }
    }
}
Python
# Loop till i <= 5
for i in range(0, 6):
    # If i = 3, then continue to the next iteration
    if i == 3:
        continue
    print(i)
JavaScript
// Loop till i <= 5
for (let i = 0; i <= 5; ++i) {
    // If i = 3, then continue to the next iteration
    if (i === 3) {
        continue;
    }
    console.log(i);
}

Output
0
1
2
4
5

Break vs Continue Statement:

Break StatementContinue Statement
The Break statement is used to exit from the loop constructs.The continue statement is not used to exit from the loop constructs.
The break statement is usually used with the switch statement, and it can also use it within the while loop, do-while loop, or the for-loop.The continue statement is not used with the switch statement, but it can be used within the while loop, do-while loop, or for-loop.
When a break statement is encountered then the control is exited from the loop construct immediately.When the continue statement is encountered then the control automatically passed to the beginning of the loop statement.
Syntax:
break;
Syntax:
continue;
Break statements uses switch and label statements.It does not use switch and label statements.
Leftover iterations are not executed after the break statement.Leftover iterations can be executed even if the continue keyword appears in a loop.


Similar Reads

If statement in Programming
An if statement is a fundamental control structure in programming languages that allows you to execute specific code blocks based on whether a condition is true or false. It is used to make decisions and control the flow of execution in your program. Table of Content What is an If Statement?Example of If Statement in ProgrammingConditional Operator
9 min read
Goto Statement in Programming
Goto statement is a control flow statement present in certain programming languages like C and C++. It enables programmers to transfer program control to a labeled section of code within the same function or block. Despite its availability, its usage is often discouraged in modern programming practices due to its potential to complicate code struct
3 min read
Switch statement in Programming
Switch statement in programming enables the execution of different code blocks based on the value of an expression, providing a structured approach to handle multiple cases efficiently. It enhances code readability and simplifies decision-making processes, making it a valuable tool for managing program flow and facilitating branching logic in softw
6 min read
If Else If Statement in Programming
If else if statement in programming allows you to check multiple conditions sequentially and execute different blocks of code based on those conditions. It's a way to handle various cases and make decisions within a program efficiently. Table of Content What is If Else If Statement?Syntax of If Else If StatementIf Else If Statement in CIf Else If S
4 min read
Nested If Else Statement in Programming
Nested If Else Statements are a fundamental concept in programming. They allow us to create more complex decision-making structures by placing one if else statement inside another. In this article, we will discuss the Nested if else statement. Table of Content What is Nested If Else Statement?Syntax of Nested If Else StatementNested If Else Stateme
6 min read
If Else Statement in Programming
An if else statement in programming is a basic programming technique that allows you to make decisions based on certain conditions. It allows your program to execute different pieces of code depending on whether the specified condition evaluates to true or false. This capability is crucial in building dynamic and functional applications. Table of C
5 min read
Break a number into 3 positive integers (p1, p2, p3) such that p2 &gt; p1 &gt; p3
Given a positive integer N, the task is to divide a number N into 3 positive integers p1, p2, p3 such that p2 &gt; p1 &gt; p3 &gt; 0. The distribution must be as equal as possible. If it is impossible to do so then print -1. Examples: Input: N = 6Output: 2 3 1Explanation: p1 = 2, p2 = 3 and p3 = 1, so 3 &gt; 2 &gt; 1 &gt; 0. Input: N = 3Output: -1
5 min read
Which Programming Language to Choose?
One of the most annoying question today is which programming language should be chosen for the sake of education/career or anything. Answer for this question to many programmers ends up with C or C++, or mostly Java but why C? why C++? Why Java?. Today many software exists, to solve a problem but also to interfere with another software. nowadays in
4 min read
Hello World Program : First program while learning Programming
In this article, I'll show you how to create your first Hello World computer program in various languages. Along with the program, comments are provided to help you better understand the terms and keywords used in theLearning program. Programming can be simplified as follows: Write the program in a text editor and save it with the correct extension
6 min read
What are Objects in Programming?
In object-oriented programming (OOP), objects are the basic entities that actually exists in the memory. Each object is based on a blueprint of attributes and behaviours (variables and functions) defined as Class. The basic purpose of a Class is to identify the common attributes and behaviours and group them as an entity that can be reused again an
2 min read
Article Tags :