Course Content
Basic of C programming
0/14
Functions, Array and Structures
0/11
C Programming Pointers
0/1
C Pointers & Arrays
0/1
C Pointers & Functions
0/1
C Memory Allocation
0/1
C Programming String
0/1
C String Functions
0/1
C Files Input/Output
0/1
C Enumeration
0/1
C Preprocessors
0/1
C Standard Library
0/1
C Programming Tutorial

## C break and continue

In this tutorial, we will learn to use break and continue statements with the help of examples.

## break statement

In all the C loops we have a way to break out of a loop at any point in time, immediately, regardless of the conditions set fo the loop.

This is done using theÂ `break`Â keyword.

In simple words, TheÂ breakÂ statement is aÂ loopÂ control statement which is used to terminate theÂ loopÂ immediately.

BasicallyÂ breakÂ statements are used in the situations when we are not sure about the actual number of iterations for theÂ loopÂ or we want to terminate theÂ loopÂ based on some condition.

Syntax :

``break;``

Note :Â TheÂ breakÂ statement is almost always used withÂ if…elseÂ statement inside the loop.

Â

### Example : break statement

``// program to use break statement inside for loop#include <stdio.h>int main() {Â Â for (int i = 1; i <= 10; i++) {Â Â // break condition     Â Â if (i == 5) {Â Â Â Â break;Â Â }Â Â printf("%d ", i);Â }return 0;}``

Output

`1 2 3 4`

In the above example ,a forÂ loop is used to print the value ofÂ iÂ in each iteration. In which aÂ ifÂ statement is used withÂ breakÂ statement in it.
The condition ofÂ if statement isÂ i == 5Â i.e. whenÂ iÂ is equal to 5 andÂ breakÂ statement is executed to terminate the loop. Hence, the output doesn’t include values greater than or equal to 5.

In C programming,Â breakÂ is also used with theÂ switchÂ statement. This will be discussed in the next tutorial.

## continue

This is one of the easiest and the most basic keywords in C/C++, which gives programmers control over loops.

TheÂ continueÂ exactly as the name suggests. Since we use this in loops, it will skip over the remaining body of the current loop, and continue to the next iteration.

We can use this inside any loops likeÂ `for`,Â `while`, orÂ `do-while`Â loops.

In Simple words,
1. The continue statement is used to skip the remaining portion of a for/while loop.
2. we continue onto the next iteration, and move to the loop condition check.
3. This is quite useful for programmers to be flexible with their approach in control loops.

Syntax :

``continue;``

Note :Â TheÂ continueÂ statement is almost always used with theÂ if…elseÂ statement.

### Example : continue statement

``// loop to print numbers 1 to 10 except 4#include <stdio.h>int main() {Â Â for (int i = 1; i <= 10; i++) {Â Â // if i is equal to 4 , continue to next iteration without printing 4.Â Â if (i == 4) {Â Â Â Â continue;Â Â }Â Â else{Â Â Â Â // otherwise print the value of i.Â Â Â Â printf("%d ", i);Â Â Â }Â }return 0;}``

Output

`1 2 3 5 6 7 8 9 10`

In the above program, we have used the theÂ forÂ loop to print the value ofÂ iÂ in each iteration. Here, notice the code,

``if (i == 4) {continue;}``

This means

• WhenÂ iÂ is equal toÂ 4, theÂ continueÂ statement skips the current iteration and starts the next iteration
• Then,Â iÂ becomesÂ 5,Â and theÂ conditionÂ is evaluated again.
• Hence,Â 5Â andÂ 6Â are printed in the next two iterations.
• Loop prints the value ofÂ iÂ until it becameÂ 11 and condition becomes fails. Then, the loop terminates.
• Note: TheÂ continueÂ statement is almost always used with decision-making statements.
• Note: TheÂ breakÂ statement terminates theÂ loop. However, theÂ continueÂ statement only skips the current iteration.
error: Content is protected !!