C Break and Continue

In C, the break and continue statements are used in loops to control the flow of the program.

The break statement is used to terminate a loop prematurely. When the break statement is encountered inside a loop, the loop is immediately terminated and control is passed to the statement immediately following the loop.

For example:

for (int i = 0; i < 10; i++) {
   if (i == 5) {
      break;
   }
   printf("%d ", i);
}

In this example, the loop will iterate from 0 to 9. However, when the value of “i” is 5, the break statement is encountered and the loop is terminated. The output of the program will be:

0 1 2 3 4

The continue statement, on the other hand, is used to skip a single iteration of a loop. When the continue statement is encountered inside a loop, the current iteration of the loop is terminated and control is passed to the next iteration.

For example:

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

In this example, the loop will iterate from 0 to 9. However, when the value of “i” is even, the continue statement is encountered and that iteration of the loop is skipped. The output of the program will be:

1 3 5 7 9

The break and continue statements are powerful tools that can be used to control the flow of a program and improve its efficiency. They are often used in loops to make decisions based on certain conditions, and can help reduce the amount of code needed to accomplish a task.

Wordpress Social Share Plugin powered by Ultimatelysocial
Wordpress Social Share Plugin powered by Ultimatelysocial