← Back to C++ Course | Chapter 4: Control Flow | Lesson 13 of 13

C++ में break और continue

break एक loop से तुरंत बाहर निकलता है, जीतने के बाद एक game से बाहर चलने जैसा, और continue सिर्फ current round skip करता है। ये आपको repeating steps के अंदर control देते हैं।
Syntax
cpp
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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. break इस्तेमाल करना और यह उम्मीद करना कि यह एक nested loop में दोनों loops से exit हो जाएगा, जब यह सिर्फ innermost से exit होता है।
  2. एक while loop में increment से पहले continue लगाना, जो एक infinite loop cause करता है।
  3. एक loop या switch के बाहर break इस्तेमाल करना, जो एक compile error है।
चैप्टर सारांश
  • Booleans और boolean expressions if, if-else, और else-if statements से decisions drive करते हैं।
  • Nested और logical conditions और switch statement ज़्यादा complex choices handle करते हैं।
  • while, do-while, और for loops code दोहराते हैं, break और continue loop को control करते हुए।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.