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

break और continue

break किसी loop को पूरी तरह रोक देता है, जैसे कोई game छोड़ना, जबकि continue सिर्फ current turn skip करके आगे बढ़ता है। दोनों आपको एक दोहराए जाने वाले task पर control देते हैं।

In this page:

  1. break and continue
Syntax
csharp
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

markup
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);
        }
    }
}
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. किसी nested loop में break उपयोग करना और इसके दोनों loops से बाहर निकलने की उम्मीद करना, जबकि यह सिर्फ सबसे अंदरूनी से बाहर निकलता है।
  2. किसी while loop में increment से पहले continue रखना, एक infinite loop का कारण बनते हुए।
  3. किसी loop या switch के बाहर break उपयोग करना, जो एक compile error है।
चैप्टर सारांश
  • if/else और switch तय करते हैं कि कौन सा code चले।
  • for, while, do-while, और foreach loops code दोहराते हैं।
  • break और continue loop flow को control करते हैं।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

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.