break और continue
break किसी loop को पूरी तरह रोक देता है, जैसे कोई game छोड़ना, जबकि continue सिर्फ current turn skip करके आगे बढ़ता है। दोनों आपको एक दोहराए जाने वाले task पर control देते हैं।
In this page:
Syntax
break; // exit the nearest loop or switch
continue; // skip to the next iteration
break and continue
break तुरंत सबसे नज़दीकी enclosing loop या switch से बाहर निकल जाता है, जबकि continue current iteration का बाकी हिस्सा skip करके अगले पर jump करता है।
दोनों आपको loops के अंदर सिर्फ loop condition से ज़्यादा बेहतर control देते हैं। इनका ज़्यादा उपयोग control flow को समझना मुश्किल बना सकता है, इसलिए इन्हें कम उपयोग करें।
Note:
बाकी loop body को किसी
if में nest करने के बजाय invalid items को जल्दी skip करने के लिए continue उपयोग करें।उदाहरण: break and continue
using System;
class Program
{
static void Main(string[] args)
{
for (int i = 1; i <= 10; i++)
{
if (i % 2 == 0)
continue; // skip even numbers
if (i > 7)
break; // stop once we pass 7
Console.WriteLine(i);
}
}
}
Login to try C/C++/Java/PHP code in the editor
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. #}
आम गलतियां
- किसी nested loop में
breakउपयोग करना और इसके दोनों loops से बाहर निकलने की उम्मीद करना, जबकि यह सिर्फ सबसे अंदरूनी से बाहर निकलता है। - किसी
whileloop में increment से पहलेcontinueरखना, एक infinite loop का कारण बनते हुए। - किसी loop या switch के बाहर
breakउपयोग करना, जो एक compile error है।
चैप्टर सारांश
if/elseऔरswitchतय करते हैं कि कौन सा code चले।for,while,do-while, औरforeachloops code दोहराते हैं।breakऔरcontinueloop flow को control करते हैं।
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: