C का goto Statement
In this page:
goto label;
// ... skipped code ...
label:
statement;
goto क्या है?
goto execution को same function में कहीं और एक labeled line पर सीधे transfer करता है, code के normal sequential flow को पूरी तरह bypass करते हुए, जो इसे powerful बनाता है लेकिन carelessly इस्तेमाल होने पर program की logic follow करना मुश्किल बनाने वाला control-flow tool भी है।
उदाहरण: What is goto?
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
goto skip;
// Print formatted text to the screen
printf("never runs");
skip:
printf("Jumped past the printf");
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Syntax और Labels
एक label बस एक identifier है जिसके बाद एक colon है जो एक statement के आगे रखा जाता है, उस line को एक valid jump target की तरह mark करते हुए, और इसे same function के अंदर कहीं भी रखा जा सकता है जहाँ उस function में कहीं और से एक goto पहुँचना चाहता है।
उदाहरण: Syntax and Labels
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare x and set it to 5
int x = 5;
if (x > 0) goto positive;
// Print formatted text to the screen
printf("not positive");
// Return 0 to signal that the program finished successfully
return 0;
positive:
printf("Positive");
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Deep Nesting से बाहर निकलना
जब एक error condition कई nested loops के अंदर गहराई में detect होती है, एक single goto cleanup; एक step में उन सबसे बाहर निकल सकता है, जबकि एक plain break सिर्फ single innermost loop से escape करता है, आपको हर बची level से manually break out करने के लिए छोड़ते हुए।
उदाहरण: Breaking Out of Deep Nesting
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Loop: repeat while the condition holds
for (int i = 0; i < 3; i++) {
// Loop: repeat while the condition holds
for (int j = 0; j < 3; j++) {
// Check whether i == 1 && j == 1
if (i == 1 && j == 1) {
goto cleanup;
}
}
}
cleanup:
printf("Exited both loops");
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Backward Jumps
क्योंकि goto एक label पर jump कर सकता है जो code में पहले आता है, backward jump करके एक loop बनाना possible है — लेकिन आपको पक्का करना होगा कि surrounding condition आखिरकार false evaluate हो, वरना वह backward jump एक runaway while loop की तरह हमेशा के लिए दोहराता रहेगा।
उदाहरण: Backward Jumps
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare i and set it to 0
int i = 0;
loopStart:
if (i < 3) {
// Print formatted text to the screen
printf("%d ", i);
i++;
goto loopStart;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
goto के साथ Best Practices
ज़्यादातर C style guides goto को legitimate uses के एक narrow set तक restrict करती हैं, मुख्य रूप से deeply nested loops से escape करना और cleanup code centralize करना (जैसे return करने से पहले कई open resources बंद करना), जबकि इसे structured loops और conditionals के लिए एक general substitute की तरह इस्तेमाल करने से दूर रहना।
उदाहरण: Best Practices with goto
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare success and set it to 0
int success = 0;
// Check whether !success
if (!success) {
goto cleanup;
}
// Print formatted text to the screen
printf("never runs");
cleanup:
printf("Centralized cleanup reached");
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- goto को एक block के बीच में या एक variable initialization से आगे jump करने के लिए इस्तेमाल करना, जो invalid या unsafe हो सकता है।
- goto को overuse करना, जो tangled code produce करता है जिसे follow करना मुश्किल है।
- एक label को गलत spell करना या इसके बाद colon भूल जाना।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: