C के break और continue
In this page:
break; // leave the loop immediately
continue; // skip to the next iteration
Loops में break Statement
break जिस loop के अंदर है उससे तुरंत exit हो जाता है, बाकी बची हर iteration को पूरी तरह skip करते हुए -- execution सीधे loop के बाद वाले पहले statement पर jump करता है, चाहे loop की original condition कुछ भी करती।
उदाहरण: The break Statement in Loops
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Loop: repeat while the condition holds
for (int i = 1; i <= 10; i++) {
// Check whether i == 5
if (i == 5) {
// Leave the switch/loop
break;
}
// Print formatted text to the screen
printf("%d ", i);
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Loops में continue Statement
continue सिर्फ current iteration की body का बाकी हिस्सा skip करता है, फिर अगली iteration शुरू करने के लिए सीधे loop के update step और condition check पर jump करता है -- break के उलट, loop खुद चलती रहती है।
उदाहरण: The continue Statement in Loops
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Loop: repeat while the condition holds
for (int i = 1; i <= 5; i++) {
// Check whether i == 3
if (i == 3) {
// Skip the rest of this iteration
continue;
}
// Print formatted text to the screen
printf("%d ", i);
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
while Loops के साथ break इस्तेमाल करना
एक while loop के अंदर, break आमतौर पर mid-loop एक specific condition detect होते ही exit करने के लिए इस्तेमाल होता है, आपको उस case के around loop की main condition को redesign करने की ज़रूरत के बिना जल्दी रोकने का एक clean तरीका देते हुए।
उदाहरण: Using break with while Loops
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare i and set it to 1
int i = 1;
// Repeat while i <= 10 is true
while (i <= 10) {
// Check whether i == 4
if (i == 4) {
// Leave the switch/loop
break;
}
// Print formatted text to the screen
printf("%d ", i);
i++;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
while Loops के साथ continue इस्तेमाल करना
एक while loop के अंदर continue इस्तेमाल करते समय, आपको यह पक्का करना होगा कि loop का counter या condition variable continue statement चलने से पहले update हो, वरना आप एक infinite loop बनाने का risk लेते हैं जो update step को हमेशा skip करता रहे।
उदाहरण: Using continue with while Loops
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare i and set it to 0
int i = 0;
// Repeat while i < 5 is true
while (i < 5) {
i++;
// Check whether i == 3
if (i == 3) {
// Skip the rest of this iteration
continue;
}
// Print formatted text to the screen
printf("%d ", i);
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Nested Loop Breaks
break सिर्फ उस single loop से exit होता है जिसके अंदर यह physically लिखा है -- अगर वह loop किसी दूसरे के अंदर nested है, outer loop पूरी तरह unaffected रहता है और normally अपनी iterations चलाता रहता है।
उदाहरण: Nested Loop Breaks
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Loop: repeat while the condition holds
for (int i = 0; i < 3; i++) {
// Loop: repeat while the condition holds
for (int j = 0; j < 3; j++) {
// Check whether j == 1
if (j == 1) {
// Leave the switch/loop
break;
}
// Print formatted text to the screen
printf("(%d,%d) ", i, j);
}
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
breakइस्तेमाल करना और यह उम्मीद करना कि यह एक nested loop में दोनों loops से exit हो जाएगा, जब यह सिर्फ innermost से exit होता है।- एक
whileloop में increment से पहलेcontinueलगाना, जो एक infinite loop cause करता है। - एक loop या switch के बाहर
breakइस्तेमाल करना, जो एक compile error है।
if,if-else, else-if ladders, nested conditions, औरswitchएक program को अलग paths के बीच चुनने देते हैं।while,do-while, औरforloops, nested loops समेत, एक condition के hold करते रहने तक code दोहराते हैं।breakऔरcontinueएक loop से बाहर निकलकर या इसकी अगली iteration पर skip करके finer control देते हैं।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: