C Programming Tutorial
About Lesson

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.

 

How break statement works?

C Working of break statement
Working of break statement in C

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 forwhile, 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.

How continue statement works?

C Working of Continue statement
Working of Continue statement in C

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.
Exercise Files
No Attachment Found
No Attachment Found
error: Content is protected !!