← Back to C Course | Chapter 14: Additional Topics | Lesson 1 of 9

C का goto Statement

goto एक label से mark की गई code में किसी दूसरी जगह सीधे jump करता है, एक book के किसी अलग page पर skip करने जैसा। यह code को follow करना मुश्किल बना सकता है, इसलिए इसे कम इस्तेमाल किया जाता है।
Syntax
c
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?

c
// 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;
}

Syntax और Labels

एक label बस एक identifier है जिसके बाद एक colon है जो एक statement के आगे रखा जाता है, उस line को एक valid jump target की तरह mark करते हुए, और इसे same function के अंदर कहीं भी रखा जा सकता है जहाँ उस function में कहीं और से एक goto पहुँचना चाहता है।

उदाहरण: Syntax and Labels

c
// 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;
}

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

c
// 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;
}

Backward Jumps

क्योंकि goto एक label पर jump कर सकता है जो code में पहले आता है, backward jump करके एक loop बनाना possible है — लेकिन आपको पक्का करना होगा कि surrounding condition आखिरकार false evaluate हो, वरना वह backward jump एक runaway while loop की तरह हमेशा के लिए दोहराता रहेगा।

उदाहरण: Backward Jumps

c
// 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;
}

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

c
// 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;
}
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. goto को एक block के बीच में या एक variable initialization से आगे jump करने के लिए इस्तेमाल करना, जो invalid या unsafe हो सकता है।
  2. goto को overuse करना, जो tangled code produce करता है जिसे follow करना मुश्किल है।
  3. एक label को गलत spell करना या इसके बाद colon भूल जाना।

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.