goto statement in C programming | सी में गो टू स्टेटमेंट | Hindi


पिछले C programming के tutorials में हमने break statement और continue statement के बारे में सीखा था यदि आपको goto statement को आसानी से सीखना हैं  तो पहले C break statmenet और C continue statement के बारे में सिखले क्योकि goto statement भी एक control statement होती हैं । 
इस tutorial में हम C programming में goto statement के बारे में समझेंगे और सीखेंगे । 

goto statement in C programming | Hindi

 goto statement एक control statement होती हैं जिसे jump statement के नाम से भी जाना जाता हैं । यदि कोई jump statement का उपयोग करने के लिए बोले तो समझ जाना की वहाँ पर goto statement का उपयोग किया जाना हैं । जैसे नाम से ही पता चलता हैं की goto statement या jump statement का उपयोग पहले से निर्धारित lable पर program control को transfer करने के लिए किया जाता हैं । किसी विशेष वजह से code के कुछ खास हिस्से को दोहराने के लिए goto statement का उपयोग किया जाता हैं । इसके उपयोग से Nested loop से program control को direct बाहर लाया जा सकता हैं जो की break statement के द्वारा नही किया जा सकता हैं क्योकि break statement केवल एक ही loop को terminate कर सकती हैं Nested loop को नही क्योकि एक loop से exit होने के बाद program control दूसरे Nested loop में चला जाता हैं पर यदि goto statement का उपयोग किया जाए तो loop के अंदर कितने भी loops क्यो न हो program control(compiler process) को तुरंत Nested loop से बाहर लाया जा सकता हैं ।
हालकी goto statement को उपयोग करने से बचा जाता हैं क्यूकी यह program की readability पर बुरा प्रभाव डालता हैं और प्रोग्राम समझने मे और modify करने में बहुत कठिन (complex) हो जाता हैं । 
चलिये goto statement के syntax से समझते हैं  
Syntax 1 

goto label_name;
..
..
label_name:

Syntax 2 

label_name:
.
.
.
goto label_name;

उपर दिये गए दोनों syntax में आप lablel देख सकते हैं lable एक user defined identifier होता हैं जो की पहले सेट करना पड़ता हैं यहाँ नाम कुछ भी ले सकते हैं । उसी नाम को goto (goto lable_name) के साथ लिखना पड़ता हैं जिससे compiler को यहाँ पता चल जाता हैं की goto के बाद किस lable पर जाना हैं उस lable के बाद जो भी code lines होती हैं उन्हे execute करने लगता हैं । 
चलिये उदाहरण से समझते हैं ।

 Examples of goto statement in C programming – 

चलिये अब goto statement को उदाहरण से समझते हैं ।   
 इस example में हम goto statement की मदत से C program के द्वारा 1 से 15 तक numbers print करेंगे । यह काम हम अब तक loops से करते आ रहे थे पर यहाँ पर हम goto statement से भी कर सकते हैं जो की नीचे दिये गए उदाहरण में देख सकते हैं । 

// Print numbers from 1 to 15 using goto statement 
#include <stdio.h> 
int main() 
{ 
int n = 1; 
label: 
    printf("%d\n",n); 
    n++; 
    if (n <= 15) {
       goto label;   
    }
   return 0; 
}

Output – 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

उपर दिये गए उदाहरण में हमने कोई भी loop का उपयोग नही किया फिर भी numbers बार बार कैसे प्रिंट हुए ?
तो इसी को समझते हैं – 
int n = 1;   :- सबसे पहले हमने एक variable n declare और initialize किया क्योकि हमे 1 से 15 तक numbers को प्रिंट करना हैं तो variable n की initial value 1 ही लेनी पड़ेगी । 
label:     :- यहाँ पर lable एक user defined identifier हैं जिसे goto lable कहते हैं जब compiler goto statement के पास जाता हैं तो goto statement compiler को पहले से सेट किए गए lable पर transfer या jump कर देता हैं जिससे lable: से code फिरसे execute होना शुरू हो जाता हैं । 

      if (n <= 15)   :-  यहाँ पर if statement का उपयोग किया गया हैं जिसमे एक condition बनाई गयी हैं की यदि variable n की वैल्यू 15 से कम या 15 के बराबर हैं तो if block में जो code हैं execute हो । तो if block में तो goto statement हैं । 
goto label;    :- जो compiler को यह निर्देश देती हैं प्रोग्राम में जहां भी lable हैं वहाँ से code execute करना शुरू करे तो compiler goto से आगे तो नही जाता हैं सीधे lable: के बाद की code lines execute करना शुरू कर देता हैं । 
lable के बाद पहले n की वैल्यू print होती हैं फिर n की value में 1 का increment होता हैं यानिकी जब पहली बार code execute होता हैं तब n की value 1 होती हैं और goto से पहले और print के बाद वैल्यू 2 हो जाती हैं फिर if condition पर codition check होती हैं और condition true मिलती हैं क्योकि n variable की value 2 हैं जो की 15 से कम हैं फिरसे goto statement execute होती हैं और goto statement फिरसे compiler को lable पर transfer कर देता हैं और फिरसे compiler lable से code execute करना start कर देता हैं यह प्रोसैस 15 numbers तक तो ऐसे ही चलता रहता हैं क्योकि तब तक if condition true होती रहती हैं पर जैसे ही 15 print होने के बाद n++ increment होता हैं तब वैल्यू 16 हो जाती हैं तो if (n <= 15)    कंडिशन false हो जाती हैं तो goto statement execute नही हो पाती तब code दोबारा नही दोहराया जाता हैं compiler return पर जाकर end हो जाता हैं । 
इस तरह से आपने देखा की यदि goto का उपयोग करे तो code को lable के द्वारा बार बार execute कराया जा सकता हैं । 
इस तरह से बिना loop के भी हमने 1 से 15 तक numbers को goto statement के द्वारा प्रिंट किया । 

Create a C program to find even or odd numbers using goto statement – 

Even Number – even नंबर वह नंबर होता हैं जिसे यदि 2 से भाग दिया जाए तो 0 शेष बचे या 2 से पूरा divide होने वाले number को even number कहते हैं । 
num % 2 == 0
Odd Number –  odd नंबर वह नंबर होता हैं जिसे यदि 2 से भाग दिया जाए तो कुछ भी 0 से बड़ा number शेष बचे या 2 से पूरा divide न होने वाले number को odd number कहते हैं ।
चलिये इसी को ही हम C programming से समझते हैं जिसके द्वारा आप किसी भी number को enter करे तो वो number even हैं की odd प्राप्त कर पाएंगे । 

// C program to check if a number is
// even or odd using goto statement
#include <stdio.h>
// function to check even or not
void checkEvenOrNot(int num)
{
  if (num % 2 == 0)
  {
    // jump to even
    goto even;
  }
  else
  {
    // jump to odd
    goto odd;
  }
even:
  printf("%d is even number.", num);
  // return if even
  return 0;
odd:
  printf("%d is odd number.", num);
  return 0;
}
int main() {
    int num;
    printf("Please enter a positive integer number:"); 
    scanf("%d",&num);
  checkEvenOrNot(num);
  return 0;
}

Output – 

Please enter a positive integer number:22
22 is even number.

इस उदाहरण में हमने checkEvenOrNot के नाम से एक function बनाया जिसे user defined function के नाम से जाना जता हैं ।  इस function में हमने if else statement का उपयोग किया हैं और उसी में ही syntax 2 की तरह goto के साथ lable नाम लिया हैं जिससे goto compiler को lable पर jump कर देता हैं और lable से code execute होता हैं । 
इसमे जब कोई user number enter करता हैं तो वह number function calling time में 
checkEvenOrNot(num); argument के रूप में दिया जाता हैं । यदि आपको functions के बारे में नही पता हैं तो आप C functions के बारे में पहले समझले । 
if (num % 2 == 0)  :- यह एक एक कंडिशन हैं जब function call किया जाता हैं तो num variable की वैल्यू condition तक पहुचती हैं । 
मतलब जैसे हमने 22 एंटर किया तब if condition कैसे होगी होगी नीचे देखिये 
if (num % 2 == 0) 
if (22 % 2 == 0)
True 

Condition true हो जाती हैं क्योकि यदि 22 को 2 से devide करे modulas निकाले तो 0 शेष बचता हैं जिसे modulas कहते हैं modulas % operator के द्वारा निकाला जाता हैं । Modulus operator के बारे में C operators के tutorial में पढ़ा जा सकता हैं । 
तो इसका मतलब if ब्लॉक में जो code हैं execute होता हैं । 

if (num % 2 == 0)
  {
    // jump to even
    goto even;
  }

इफ ब्लॉक में goto even lable पर program control को jump करा देता हैं जिससे 

even:
  printf("%d is even number.", num);
  // return if even
  return 0;

even lable से code execute होना शुरू हो जाता हैं और return 0; तक चलता हैं यदि return न दे तो compiler odd lable से भी code को execute कर देगा । 
इस तरह से यदि वैल्यू 21 enter करे तो if condition false होगी और else block execute होगा । 

else
  {
    // jump to odd
    goto odd;
  }

else में goto odd वाले lable पर jump करा देगा और odd lable से code execute होगा इस तरह से goto statement का उपयोग program control को किसी भी program के code part को execute करने के लिए jump कराया जा सकता हैं । 

Direct exit from Nested loop using goto statement in C programming – 

#include <stdio.h>  
int main()   
{  
  int i, j, k;    
  for(i=1; i<10; i++)  
  {  
    for(j=1;j<5;j++)  
    {   k=1;
       while (k<3)  
      {  
        printf("%d %d %d\n",i,j,k);  
        if(j == 3)  
        {  
          goto exit_now;   
        }  
     k++;
      }  
    }  
  }  
  exit_now:   
  printf("Exit from all loops |Statements after loops"); 
}

Output – 

1 1 1
1 1 2
1 2 1
1 2 2
1 3 1
Exit from all loops |Statements after loops

उपर दिये गए Nested loop में हमने if statement का उपयोग किया और एक कंडिशन बनाई j == 3 जब j variable की  वैल्यू 3 हो तो goto statement चले goto के साथ goto exit_now ; lable नाम दिया गया हैं और loop के बाद exit_now:   lable को पहले से सेट किया हुआ हैं जब भी किस भी loop में Nested loop के अंदर j की वैल्यू 3 होती हैं तो goto , program control को exit_now: lable पर jump करा देता है और वहाँ से statement execute होनी शुरू हो जाती हैं । 
इस तरह से आप देख सकते हैं की Nested loop से direct बाहर program control को ला सकते है । 

इस तरह से goto statement का C programming में उपयोग किया जा सकता हैं पर इसे उपयोग करने से बचा जाता हैं क्यूकी ये प्रोग्राम को ओर ज्यादा कठिन (complex)  कर देता हैं । 
और समझ नही आता हैं की कहाँ पे क्या हो रहा हैं । 
जैसे – 

one:
for (i = 0; i < number; ++i)
{
    test += i;
    goto two;
}
two: 
if (test > 5) {
  goto three;
}
...

उपर दिये गए उदाहरण में आप देख सकते हैं की कितना कठिन ( complex)  हो जा रहा हैं । 
goto के बिना भी program बनाए जा सकते हैं । जैसे even और odd के program को सीधे सीधे हम if else statement से भी बना सकते हैं  । 

Create a C program to find out even or odd numbers using the if-else statement -

// C program to check if a number is
// even or not using if else statement
#include <stdio.h>
// function to check even or not
void checkEvenOrNot(int num)
{
  if (num % 2 == 0)
  {
  printf("%d is even number.", num);
  }
  else
  {
    printf("%d is odd number.", num);
  }
return 0;
}
int main() {
    int num;
    printf("Please enter a positive integer number:"); 
    scanf("%d",&num);
  checkEvenOrNot(num);
  return 0;
}

Output – 

Please enter a positive integer number:22
22 is even number.

इस उदाहरण मे बिना goto statement के भी हमने आसानी से even ओर odd number find करने का एक C program बना लिया हैं जो की समझने मे भी काफी आसान हैं ओर लिखने में । 
फिर भी यदि आपको goto statement का उपयोग करना हैं तो बिलकुल आप कर सकते हैं क्यूकी goto statement के द्वारा बहुत कुछ किया जा सकता हैं ।
इस तरह से C programming में goto statement का उपयोग किया जा सकता हैं । 


Please Share

Recommended Posts:-