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

C के break और continue

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

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

Loops में continue Statement

continue सिर्फ current iteration की body का बाकी हिस्सा skip करता है, फिर अगली iteration शुरू करने के लिए सीधे loop के update step और condition check पर jump करता है -- break के उलट, loop खुद चलती रहती है।

उदाहरण: The continue Statement in Loops

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

while Loops के साथ break इस्तेमाल करना

एक while loop के अंदर, break आमतौर पर mid-loop एक specific condition detect होते ही exit करने के लिए इस्तेमाल होता है, आपको उस case के around loop की main condition को redesign करने की ज़रूरत के बिना जल्दी रोकने का एक clean तरीका देते हुए।

उदाहरण: Using break with while Loops

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

while Loops के साथ continue इस्तेमाल करना

एक while loop के अंदर continue इस्तेमाल करते समय, आपको यह पक्का करना होगा कि loop का counter या condition variable continue statement चलने से पहले update हो, वरना आप एक infinite loop बनाने का risk लेते हैं जो update step को हमेशा skip करता रहे।

उदाहरण: Using continue with while Loops

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

Nested Loop Breaks

break सिर्फ उस single loop से exit होता है जिसके अंदर यह physically लिखा है -- अगर वह loop किसी दूसरे के अंदर nested है, outer loop पूरी तरह unaffected रहता है और normally अपनी iterations चलाता रहता है।

उदाहरण: Nested Loop Breaks

c
// 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;
}
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 है।
चैप्टर सारांश
  • if, if-else, else-if ladders, nested conditions, और switch एक program को अलग paths के बीच चुनने देते हैं।
  • while, do-while, और for loops, nested loops समेत, एक condition के hold करते रहने तक code दोहराते हैं।
  • break और continue एक loop से बाहर निकलकर या इसकी अगली iteration पर skip करके finer 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.