Skip to main content

Break and Continue in C

The break statement is used to exit a loop early, before the loop's conditional expression is false.

The continue statement is used to skip an iteration of a loop, which means that the rest of the code in the current iteration is not executed and the next iteration begins.

Both break and continue are typically used inside loops, such as for and while loops.

The "break" statement

The syntax for the break statement in C is:

break;

When the break statement is encountered inside a loop or switch statement, the program jumps out of the loop or switch and continues executing the code that follows the loop or switch.

Advantages of using "break"

  • Exiting a loop early: The break statement can be used to exit a loop early, before the loop's conditional expression is false. This can be useful when a certain condition is met and you no longer need to continue executing the loop.

  • Simplifying complex control flow: The break statement can be used to simplify complex control flow, making the code more readable and easier to understand.

Example of using "break" in loop

Here's an example of using the break statement inside a while loop:

#include <stdio.h>

int main()
{
int x = 0;
while (x < 10)
{
if (x == 5)
{
break;
}
x++;
}
printf("x = %d", x);
return 0;
}
Output:

Explanation

In this example, the loop will continue to execute as long as the variable x is less than 10. However, when x becomes equal to 5, the break statement is encountered, and the loop is exited.

The program then continues executing the code that follows the loop.

Example of using "break" in switch

Here's an example of using the break statement inside a switch statement in C:

#include <stdio.h>

int main()
{
int x = 2;
switch (x)
{
case 1:
printf("x is 1\n");
break;
case 2:
printf("x is 2\n");
break;
case 3:
printf("x is 3\n");
break;
default:
printf("x is not 1, 2, or 3\n");
}
return 0;
}
Output:

Explanation

The break statement at the end of each case block is important because it tells the program to exit the switch statement when a match is found, so that the code in the next case blocks is not executed. If the break statement is not used, the program will continue executing the code in the next case blocks until a break statement or the end of the switch statement is reached.

Output:

x is 2

"break" statement in nested loops

It is also important to note that the break statement will only exit the innermost loop that it is in. This means that if a break statement is encountered inside a nested loop, it will only exit the inner loop and not any outer loops that the inner loop may be nested within.

Here's an example to illustrate this:

#include <stdio.h>

int main()
{
int i, j;

for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
printf("i = %d, j = %d\n", i, j);
if (i == 1 && j == 1)
{
break;
}
}
}
return 0;
}
Output:

Explanation

In this example, we have an outer for loop that iterates 3 times, and an inner for loop that also iterates 3 times. When i is 1 and j is 1, the break statement is encountered, and the inner loop is exited. However, the outer loop continues to execute.

The program prints the following output:

i = 0, j = 0
i = 0, j = 1
i = 0, j = 2
i = 1, j = 0
i = 2, j = 0
i = 2, j = 1
i = 2, j = 2

As you can see from the output, the inner loop is exited when i is 1 and j is 1, but the outer loop continues to execute, printing all the other values of i and j.

If you want to exit both loops, you can use labeled break statement.

outer_loop:
for (i = 0; i < 3; i++) {
inner_loop:
for (j = 0; j < 3; j++) {
printf("i = %d, j = %d\n", i, j);
if (i == 1 && j == 1) {
break outer_loop;
}
}
}

This will exit both outer and inner loop when i is 1 and j is 1

tip

When using the break statement inside nested loops, it's important to keep in mind that it will only exit the innermost loop that it is in, and any outer loops will continue to execute. To exit multiple loops at the same time, you can use labeled break statement to specify which loop to exit.

The "continue" statement

The continue statement is used to skip an iteration of a loop. When the continue statement is encountered inside a loop, the rest of the code in the current iteration is not executed and the next iteration begins.

It can be used in both for and while loops.

The syntax for the continue statement in C is:

continue;

Advantages of using "continue"

  • Skipping an iteration: The continue statement can be used to skip an iteration of a loop, which can be useful when a certain condition is met and the current iteration does not need to be executed.

  • Simplifying complex control flow: Like the break statement, the continue statement can be used to simplify complex control flow, making the code more readable and easier to understand.

  • Improving performance: Using continue statement can also improve the performance of the program by avoiding unnecessary computations or operations.

Example of "continue" statement in "for" Loop

Here's an example of using the continue statement inside a for loop:

#include <stdio.h>

int main()
{
int i = 0;

while (i < 10)
{
i++;
if (i % 2 == 0)
{
continue;
}
printf("%d ", i);
}

return 0;
}
Output:

Explanation

In this example, the for loop iterates from 0 to 9. In each iteration, the value of i is checked to see if it is even or odd. If the value of i is even, the continue statement is encountered, and the rest of the code in the current iteration is skipped, and the next iteration begins. If the value of i is odd, the printf statement is executed and the value of i is printed.

The output of the above code will be:

1 3 5 7 9 

As you can see, the even numbers are skipped due to the continue statement, only the odd numbers were printed.

Example of "continue" statement in "while" Loop

Here is an example of using the continue statement inside a while loop in C:

#include <stdio.h>

int main()
{
int i;
for (int i = 0; i < 10; i++)
{
if (i % 2 == 0)
{
continue;
}
printf("%d ", i);
}
return 0;
}
Output:

Explanation

If the value of i is even, the continue statement is encountered, and the rest of the code in the current iteration is skipped, and the next iteration begins.

If the value of i is odd, the printf statement is executed and the value of i is printed.

The output of the above code will be :

1 3 5 7 9 

As you can see, the even numbers are skipped due to the continue statement, only the odd numbers were printed.

"continue" statement in Nested loops

It is important to note that continue statement will only skip the current iteration of the innermost loop it is in, when it is used in nested loops. The outer loop continues to execute as normal.

Here's an example that illustrates this:

#include <stdio.h>

int main()
{
int i, j;
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
if (i == 1 && j == 1)
{
continue;
}
printf("i = %d, j = %d\n", i, j);
}
}
return 0;
}
Output:

Explanation

In this example, we have an outer for loop that iterates 3 times, and an inner for loop that also iterates 3 times. In each iteration of the inner loop, the values of i and j are checked, and if they are both equal to 1, the continue statement is encountered, and the rest of the code in the current iteration of the inner loop is skipped, and the next iteration begins.

The output of the above code will be:

i = 0, j = 0
i = 0, j = 1
i = 0, j = 2
i = 1, j = 0
i = 1, j = 2
i = 2, j = 0
i = 2, j = 1
i = 2, j = 2

As you can see, the inner loop's iteration where i is 1 and j is 1 is skipped and not printed. However, the outer loop continues to execute, and the program prints the values of i and j for all other iterations.