Jumping statements: goto, break, continue

break loop को तुरंत छोड़ देता है, continue सिर्फ़ इस round का बाक़ी हिस्सा skip करता है, और goto किसी label पर कूदता है: legal है, पर इससे बचा जाता है।

5 min read · 9 cards · 5 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

यह कहाँ रुकता है?

5 तक गिनने वाले एक loop में एक extra line है:

for (i = 1; i <= 5; i++) {

if (i == 3) break;

printf("%d ", i);

}

Screen पर क्या आता है?

Think first

पहले आपका अंदाज़ा

जो print होगा, वह हूबहू type कीजिए।

Show the answer

1 2। i = 3 पर break printf चलने से पहले ही पूरा loop ख़त्म कर देता है।

Theory

break, continue, goto

break; जिस loop या switch में है, उसे तुरंत छोड़ देता है। continue; सिर्फ़ इस round का बाक़ी हिस्सा skip करता है; for loop में update फिर भी चलता है। goto label; उसी function में label: पर कूदता है: legal है, पर spaghetti code बनाता है, इसलिए इससे बचा जाता है।

Quiz

शुरुआत वाले loop में break की जगह continue रखिए। अब क्या print होता है?

  1. 1 2 4 5
  2. 1 2
  3. 1 2 3 4 5
  4. 3
Show the answer

1 2 4 5

continue सिर्फ़ round 3 का printf skip करता है, और loop 4 और 5 के साथ आगे चलता है। 1 2 तो break देता है: break छोड़ देता है, continue skip करता है।

Practical

Nested loop के अंदर break

#include <stdio.h>

int main() {
    int i, j;

    for (i = 1; i <= 3; i++) {
        for (j = 1; j <= 3; j++) {
            if (j == 2)
                break;
            printf("%d%d ", i, j);
        }
    }
    return 0;
}

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Quiz

ऊपर वाला program क्या print करता है?

  1. 11 21 31
  2. 11
  3. 11 12 13 21 22 23 31 32 33
  4. 11 12
Show the answer

11 21 31

break सिर्फ़ अंदर वाला loop छोड़ता है, इसलिए बाहर वाले हर round में सिर्फ़ उसकी j = 1 वाली जोड़ी print होती है। जवाब 11 मान लेता है कि break दोनों loops छोड़ देता है; ऐसा कभी नहीं होता।

Think first

continue का जाल

यहाँ क्या होता है? अपना जवाब type कीजिए।

i = 0;

while (i < 5) {

if (i == 2) continue;

printf("%d ", i);

i++;

}

Show the answer

0 1, और फिर यह हमेशा चलता रहता है: i = 2 पर continue i++ चलने से पहले ही वापस जाँच पर कूद जाता है, इसलिए i 2 ही रहता है। for loop में यह ख़तरा नहीं: उसका update फिर भी चलता है।

Think first

Exam-style: break बनाम continue

break और continue में 3 points में अंतर बताइए, फिर मिलाइए।

Show the answer
  • असर: break loop ख़त्म कर देता है; continue सिर्फ़ इस round को।
  • उसके बाद: break loop के बाहर जाता है; continue अगले round पर।
  • कहाँ: break loops और switch दोनों में चलता है; continue सिर्फ़ loops में।

दोनों का एक example भी लिखिए।

Summary

Key takeaways

  • break loop या switch को तुरंत छोड़ता है, सिर्फ़ एक level।
  • continue इस round का बाक़ी हिस्सा skip करता है; loop चलता रहता है।
  • goto किसी label पर कूदता है: legal है, पर इससे बचा जाता है।
  • याद रखने का hook: break छोड़ता है, continue skip करता है।

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Decision Making and Iterative statements

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Jumping statements: goto, break, continue · Computer Programming and Programming Methodology (CPPM) · Gri-Learn