C++ में break और continue
In this page:
break; // leave the loop immediately
continue; // skip to the next iteration
break Statement
break जिस loop के अंदर है उसे तुरंत terminate कर देता है, loop की closing brace के बाद वाली पहली line पर सीधे jump करते हुए, चाहे loop की normal condition इसे चलाती रहती।
यह आमतौर पर एक target value मिलते ही search करना बंद करने के लिए इस्तेमाल होता है, wasted iterations से बचते हुए।
उदाहरण: The break Statement
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Loop: repeat while the condition holds
for (int i = 0; i < 10; i++) {
// Check whether i == 5
if (i == 5) {
// Leave the switch/loop
break;
}
// Print to the console with cout
std::cout << i << " ";
}
// Print to the console with cout
std::cout << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
continue Statement
continue सिर्फ current iteration में बचा code skip करता है और सीधे loop के अगले pass पर jump करता है, break की तरह पूरी तरह loop से exit होने की बजाय।
यह filtering के लिए useful है — उदाहरण के लिए, एक loop में negative numbers skip करना जिसे सिर्फ positive ones process करने हैं।
उदाहरण: The continue Statement
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Loop: repeat while the condition holds
for (int i = 0; i < 5; i++) {
// Check whether i == 2
if (i == 2) {
// Skip the rest of this iteration
continue;
}
// Print to the console with cout
std::cout << i << " ";
}
// Print to the console with cout
std::cout << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Nested Loops में break
Nested loops के अंदर, break सिर्फ उस innermost loop से exit होता है जिसमें यह directly लिखा है — outer loop unaffected रहता है और normally iterate करता रहता है।
अगर आपको एक साथ कई nested loops से escape करना हो, एक common approach outer loop में check किया गया एक boolean flag है, क्योंकि C++ में कोई built-in labeled break नहीं है।
उदाहरण: break in Nested Loops
// Include std::cout and std::cin
#include <iostream>
// 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 to the console with cout
std::cout << i << "," << j << " ";
}
}
// Print to the console with cout
std::cout << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Nested Loops में continue
Similarly, एक nested loop के अंदर continue सिर्फ उस loop के अंदर आगे skip करता है जिसमें यह directly है, outer loop की iteration count untouched छोड़ते हुए।
Inner loop को affect करने बनाम outer loop को affect करने के बीच का यह फर्क loops deeply nested होने पर confusion का एक common source है।
उदाहरण: continue in Nested Loops
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Loop: repeat while the condition holds
for (int i = 0; i < 2; i++) {
// Loop: repeat while the condition holds
for (int j = 0; j < 3; j++) {
// Check whether j == 1
if (j == 1) {
// Skip the rest of this iteration
continue;
}
// Print to the console with cout
std::cout << i << "," << j << " ";
}
}
// Print to the console with cout
std::cout << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
User Input के साथ Control
User input के आधार पर conditions के साथ break और continue combine करना एक program को runtime पर dynamically respond करने देता है — उदाहरण के लिए, user के quit type करते ही एक input loop से बाहर निकलना, या invalid entries चुपचाप skip करने और prompt करते रहने के लिए continue इस्तेमाल करना।
उदाहरण: Control with User Input
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
int hardcodedInputs[] = {3, 5, -1, 7};
// Loop: repeat while the condition holds
for (int i = 0; i < 4; i++) {
// Declare value and set it to hardcodedInputs[i]
int value = hardcodedInputs[i];
// Check whether value < 0
if (value < 0) {
// Leave the switch/loop
break;
}
// Print to the console with cout
std::cout << value << " ";
}
// Print to the console with cout
std::cout << std::endl;
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 है।
- Booleans और boolean expressions
if,if-else, औरelse-ifstatements से decisions drive करते हैं। - Nested और logical conditions और
switchstatement ज़्यादा complex choices handle करते हैं। while,do-while, औरforloops code दोहराते हैं,breakऔरcontinueloop को control करते हुए।
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: