Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Break/continue:

Sure! Let's discuss the break and continue statements in C using simple language:

Break Statement:

The break statement in C is used to immediately exit from a loop, skipping any remaining code inside the loop. It's commonly used to terminate a loop prematurely when a certain condition is met.

Here's a simple example:

#include <stdio.h>

int main() {
int i;

for (i = 0; i < 5; i++) {
printf("%d\n", i);
if (i == 2) {
break; // Exit the loop when i equals 2
}
}

return 0;
}

In this example:

So, the output of this code will be:

0
1
2

Continue Statement:

The continue statement in C is used to skip the remaining code inside a loop and proceed to the next iteration of the loop. It's handy when you want to skip certain iterations based on a condition without terminating the loop entirely.

Here's a simple example:

#include <stdio.h>

int main() {
int i;

for (i = 0; i < 5; i++) {
if (i == 2) {
continue; // Skip the rest of the loop when i equals 2
}
printf("%d\n", i);
}

return 0;
}

In this Examples

The output of this code will be:

0
1
3
4

This is because i equals 2 is skipped due to the continue statement, and the loop continues with the next iteration.



Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java