C break and continue
Understand how break and continue alter loop behavior in C programming with real examples.
Break: Exits the loop immediately when a condition is met.
Continue: Skips the current iteration and jumps to the next one.
Break Statement Example
#include <stdio.h>
int main() {
for(int i = 1; i <= 10; i++) {
if(i == 5)
break;
printf("%d\n", i);
}
return 0;
}
This prints numbers 1 to 4. When i equals 5, the loop is terminated.
Continue Statement Example
#include <stdio.h>
int main() {
for(int i = 1; i <= 5; i++) {
if(i == 3)
continue;
printf("%d\n", i);
}
return 0;
}
This program skips printing the number 3. Output is 1, 2, 4, 5.
Break: जब कोई शर्त पूरी होती है, तब loop को तुरंत रोक देता है।
Continue: वर्तमान iteration को छोड़कर loop के अगले चक्कर पर चला जाता है।
Break स्टेटमेंट उदाहरण
#include <stdio.h>
int main() {
for(int i = 1; i <= 10; i++) {
if(i == 5)
break;
printf("%d\n", i);
}
return 0;
}
यह केवल 1 से 4 तक के नंबर प्रिंट करता है। जब i = 5 होता है, loop रुक जाता है।
Continue स्टेटमेंट उदाहरण
#include <stdio.h>
int main() {
for(int i = 1; i <= 5; i++) {
if(i == 3)
continue;
printf("%d\n", i);
}
return 0;
}
यह प्रोग्राम 3 को छोड़कर बाकी नंबर प्रिंट करता है: 1, 2, 4, 5।