Introduction
Programming में सामान्य रूप से statements एक fixed order में execute होते हैं। अगर program में decision making या repetition की जरूरत होती है, तो हम if, loops, switch आदि control structures का उपयोग करते हैं। ये structured programming के तरीके हैं, जो program को easy to read और easy to manage बनाते हैं।
लेकिन C language में एक ऐसा statement भी है जो program control को एक जगह से सीधे दूसरी जगह भेज सकता है। इस statement को goto statement कहा जाता है।
goto statement program के normal flow को बदल देता है। जहाँ बाकी control structures condition, loop या block के आधार पर execution को control करते हैं, वहीं goto statement एक label की मदद से control को सीधे किसी specific statement पर transfer कर देता है।
यह concept समझना जरूरी है क्योंकि:
- exam में इसकी definition, syntax और example पूछे जा सकते हैं
- output-based questions में goto का flow confusing हो सकता है
- practical programming में इसका limited use होता है, लेकिन conceptually यह program flow को समझने में मदद करता है
हालाँकि modern programming में goto का use कम किया जाता है, क्योंकि इससे program unstructured हो सकता है और code को समझना difficult हो सकता है। फिर भी, C language के syllabus में यह topic important है, इसलिए इसका logic clear होना चाहिए।
Definition
goto statement एक unconditional jump statement है, जो program control को सीधे किसी labeled statement पर transfer कर देता है।
Syntax
goto label_name;
/* statements */
label_name:
statement;
Important Points
- goto के साथ एक label use किया जाता है
- label program में उस जगह को mark करता है जहाँ control जाना है
- goto एक unconditional jump है
- इसका ज्यादा use program को confusing बना सकता है
- label के बाद colon
:लगाया जाता है
Example 1: Simple Jump
#include <stdio.h>
int main()
{
printf("First Line\n");
goto end;
printf("Second Line\n");
end:
printf("Last Line");
return 0;
}
Output:
First Line
Last Line
Explanation:
- सबसे पहले
First Lineprint हुई - फिर
goto end;execute हुआ - control सीधे
end:label पर चला गया - इसलिए
Second Lineprint नहीं हुई - उसके बाद
Last Lineprint हुई
Example 2: Skip a Part of Program
#include <stdio.h>
int main()
{
int a = 5;
if (a > 0)
goto positive;
printf("Negative Number\n");
positive:
printf("Positive Number");
return 0;
}
Output:
Positive Number
Explanation:
a = 5, इसलिए conditiona > 0true है- control
goto positive;के कारण सीधेpositive:label पर चला गया Negative Numberवाली line skip हो गई- इसलिए केवल
Positive Numberprint हुआ
Example 3: goto with Loop-like Behavior
#include <stdio.h>
int main()
{
int i = 1;
start:
printf("%d ", i);
i++;
if (i <= 5)
goto start;
return 0;
}
Output:
1 2 3 4 5
Explanation:
- पहले
i = 1print हुआ - फिर
iincrement हुआ - जब तक
i <= 5रहा, control बार-बारstart:label पर जाता रहा - इस तरह goto ने loop जैसा behavior दिया
- जब
i = 6हुआ, condition false हो गई और program stop हो गया
Example 4: Infinite Loop Possibility
#include <stdio.h>
int main()
{
start:
printf("Hello\n");
goto start;
return 0;
}
Output:
Hello
Hello
Hello
...
Explanation:
start:label परHelloprint होता है- फिर
goto start;control को वापस उसी label पर भेज देता है - यह process बार-बार repeat होती रहती है
- इसलिए यह infinite loop बन जाता hai
Technical Understanding
goto statement का उपयोग वहाँ किया जा सकता है जहाँ program में direct jump की जरूरत हो।
लेकिन structured programming में if, while, for, break, continue आदि का use अधिक suitable माना जाता है।
goto का excessive use करने से:
- code difficult हो जाता है
- debugging मुश्किल हो जाती है
- flow समझना कठिन हो जाता है
इसी कारण इसे अक्सर unstructured programming statement भी कहा जाता है।
Difference Between goto and break
| goto | break |
|---|---|
| Control को किसी label पर भेजता है | Loop या switch को terminate करता है |
| Unconditional jump है | Specific structure के अंदर काम करता है |
| Program flow को कहीं भी मोड़ सकता है | केवल current loop/switch से बाहर आता है |
Real Understanding (Technical Use)
goto conceptually इन situations को समझने में मदद करता है:
- direct control transfer
- unstructured flow
- label-based jump
- loop-like manual repetition
लेकिन practical coding में generally:
- loop
- break
- continue
- function calls
इनका use better माना जाता है।
Exam Points
- goto एक unconditional jump statement है
- यह control को labeled statement पर भेजता है
- label के बाद colon
:लगाया जाता है - goto program flow को अचानक बदल देता है
- इसका ज्यादा use code को complex बना देता है
- exam में syntax, definition और output-based example पूछे जा सकते हैं