Jumping statements: goto, break, continue

break loop को पूरी तरह छोड़ देता है, continue सिर्फ मौजूदा round छोड़ता है, और goto कहीं भी किसी label पर कूदता है, legal पर बहिष्कृत, वे तीन तरीके जिनसे C सामान्य flow में दख़ल देता है।

10 min read · 10 cards · 3 checks

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


Theory

मिल गया। ढूँढते क्यों रहें?

Marks program 60 records में roll number 42 ढूँढता है। Loop उसे position 7 पर पा लेता है... और फिर ईमानदारी से records 8 से 60 तक जाँचता है, कुछ न सीखते हुए।

एक और दिन: class का average निकालना, पर absent students -1 से चिह्नित हैं, और वे total को खराब नहीं करने चाहिए।

दो अलग ज़रूरतें: loop को पूरी तरह छोड़ना, और सिर्फ यह round छोड़ना। C दोनों देता है, साथ में एक जंगली पुरखा जिसे उसके पिंजरे में ही रखना बेहतर।

Theory

कतार छोड़ना बनाम एक counter छोड़ना

आप counters की एक कतार पर चल रहे हैं। break hall से बाहर निकल जाना है: जो भी counters बचे हों, आप उन सबसे फ़ारिग़। continue एक counter छोड़ना है ("यह बंद है, आगे बढ़ो") बाकी के लिए कतार में बने रहते हुए। और goto एक label चिल्लाकर इमारत में कहीं भी teleport होना है, रोमांचक, और ठीक वैसे इमारतें अफ़रा-तफ़री में उतरती हैं।

Theory

तीनों, औपचारिक रूप से

  • break; enclosing loop (या switch) को तुरंत खत्म करता है; execution उसके closing brace के बाद से फिर शुरू होता है। Nested loops में यह सिर्फ एक level छोड़ता है।
  • continue; मौजूदा iteration का बाकी हिस्सा छोड़ देता है और अगले round पर कूदता है। एक for loop में, update (i++) फिर भी चलता है, फिर condition test होती है।
  • goto label; function में कहीं भी label: पर बिना शर्त कूदता है। Legal C, पर यह sequence/selection/iteration अनुशासन को spaghetti में घोल देता है; आधुनिक code इसे लगभग कभी allow नहीं करता (एक क्लासिक माफ़ी: deep nesting से बचना)।

Practical

break ढूँढता है, continue छानता है

#include <stdio.h>

int main() {
    int marks[6] = {70, -1, 55, 90, -1, 62};
    int i, total = 0, present = 0;

    for (i = 0; i < 6; i++) {
        if (marks[i] == -1)
            continue;          /* absent: skip this round */
        total += marks[i];
        present++;
    }
    printf("Average of %d students: %d\n", present, total / present);
    return 0;
}

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

Think first

Filter को trace कीजिए

ऊपर वाले loop पर चलिए। कौन से indexes total += तक पहुँचते हैं, अंत में total और present क्या हैं, और क्या print होता है?

Show the answer

Indexes 1 और 4 continue से टकराते हैं और कुछ योगदान नहीं देते। Indexes 0, 2, 3, 5, 70 + 55 + 90 + 62 जोड़ते हैं: total = 277, present = 4, print होता है Average of 4 students: 69 (integer division)।

ध्यान दीजिए continue ने क्या नहीं किया: उसने कभी loop खत्म नहीं किया; उसने सिर्फ absentees को fast-forward किया। वही फ़र्क़ पूरा exam सवाल है।

Quiz

10 items process करते एक loop के अंदर, item 4 पर break से टकराने और continue से टकराने में क्या फ़र्क़ है?

  1. break, items 4-10 को पूरी तरह छोड़ देता है; continue, item 4 छोड़ता है और 5-10 process करता है
  2. दोनों loop रोक देते हैं, break बस तेज़ी से करता है
  3. continue loop से बाहर निकलता है; break एक item छोड़ता है
  4. break, loop को item 1 से फिर शुरू करता है
Show the answer

break, items 4-10 को पूरी तरह छोड़ देता है; continue, item 4 छोड़ता है और 5-10 process करता है

break बाहर है: loop खत्म, items 5 से 10 कभी नहीं होते। continue अगला है: सिर्फ round 4 का बाकी छोड़ा जाता है, और loop जीता रहता है। Option C इन्हें उलट देता है, मानक trap। एक-line याद: break = hall छोड़ो, continue = एक counter छोड़ो।

Quiz

एक break, nested जोड़ी के INNER loop के अंदर execute होता है। क्या रुकता है?

  1. सिर्फ inner loop; outer अपना अगला iteration जारी रखता है
  2. दोनों loops एक साथ रुक जाते हैं
  3. सिर्फ outer loop रुकता है
  4. पूरा program खत्म हो जाता है
Show the answer

सिर्फ inner loop; outer अपना अगला iteration जारी रखता है

break ठीक एक level छोड़ता है: वह loop जो सीधे उसे घेरता है। Outer loop को कभी पता नहीं चलता और वह अपने अगले round के साथ आगे बढ़ता है। दोनों levels से निकलने के लिए एक flag variable चाहिए (या, वह एक क्लासिक माफ़ किया गया goto)। "क्या break दोनों nested loops से निकलता है?" एक भरोसेमंद exam चाल है, और जवाब ना है।

Watch out

Marks कहाँ कटते हैं

Trace सवालों में break और continue उलटना, धीमे चलिए और हर एक को label कीजिए। यह मानना कि break सारे nested levels से निकलता है: सिर्फ एक level। यह लिखना कि continue, for का update छोड़ता है: i++ फिर भी चलता है, इसीलिए for loop में continue शायद ही कभी लटकता है, पर एक while loop में manual i++ से पहले रखा continue हमेशा के लिए loop कर सकता है, एक सूक्ष्म पसंदीदा। और exams में, goto को "legal पर बहिष्कृत: यह spaghetti code बनाता है" कहिए।

Theory

Unit 2 बंद होता है

फ़ैसले, loops, nesting, और अब आपातकालीन exits: marks program अपने flow को पूरी तरह नियंत्रित करता है। जो अभी भी उसके पास नहीं है वह एक ढंग की MEMORY है: 60 marks को अभी 60 variables चाहिए। Unit 3 इसे एक झटके में ठीक करता है arrays से, एक नाम, 60 numbered boxes, और आपने जो हर loop सीखा है वह दस गुना ज़्यादा उपयोगी हो जाता है।

Summary

Key takeaways

  • break: enclosing loop या switch को तुरंत छोड़ें, सिर्फ एक nested level।
  • continue: इस round का बाकी छोड़ें; loop (और for का update) चलता रहता है।
  • goto label: बिना शर्त कूद; legal, पर spaghetti, बहिष्कृत।
  • इस्तेमाल: break एक कामयाब search खत्म करता है; continue खराब records छानता है।
  • while loop के manual increment से पहले continue लटका सकता है, for loops सुरक्षित हैं।
  • याद रखने का hook: hall छोड़ो (break), एक counter छोड़ो (continue)।

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