C goto Statement In Hindi

📔 : C 🔗

C में आपने break , continue के बारे में पढ़ा और समझा होगा जहाँ पर हम अपनी need के according program / loop execution के flow को change कर सकते थे। break , continue के through हम किसी loop execution को ही manipulate कर सकते हैं , अब अगर हमें कही पर normal flow को ही manipulate करना पड़ेंगे तो क्या करेंगे ? ऐसी situation में हम goto use करते हैं।


C में goto keyword का use हम Program में किसी दूसरे section (code of block) पर move करने के लिए करते हैं। Targeted Section को हम label के through define करते हैं और उसी Specified label को goto keyword के साथ target करते हैं।

C goto Syntax

goto targeted_level;
level_name : {
  //do something here;
}

हालाँकि आप single line goto statement define करने के लिए curly brackets { } नहीं भी लगाएं तो भी कोई बात नहीं।

C goto example

CopyFullscreenClose FullscreenRun
#include <stdio.h>
int main() {
  ineligible : {
    printf("You are not eligible to vote!\n");
  }
    
  int age;
  printf("Enter your age:");    
  scanf("%d", &age);  
  if (age < 18) {
    goto ineligible;   
  }
  else {
    printf("You are eligible to vote!"); 
  } 
    
  return 0;
}
Output
You are not eligible to vote!
Enter your age:12
You are not eligible to vote!
Enter your age:14
You are not eligible to vote!
Enter your age:24
You are eligible to vote!

तो कुछ इस तरह से C में goto statement का use किया जाता है।

Example में \n का use line break करने के लिए किया गया है ।

C goto with loop

One more thing , जब हम for Loop या while Loop के अंदर से बाहर define किये गए labels को Access करते हैं तो जैसे ही goto statement execute होता है तो loop भी break हो जाता है , means हम पूरी तरह से Loop को exit कर देतें हैं।

CopyFullscreenClose FullscreenRun
#include <stdio.h>
int main() {
  for(int i=1; i<=5; i++) {
    // check condition to jumpp on other block of code.
    if(i == 3) 
      goto inside_loop;
    
    printf("Loop count : %d\n" , i);
  }   
  
  // define block of code.
  inside_loop : {
    printf("Loop end");
  }
  return 0;
}
Output
Loop count : 1
Loop count : 2
Loop end

I Hope, अब आपको C में goto के बारे में अच्छे से समझ आ गया होगा।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! My name is Rahul Kumar Rajput. I'm a back end web developer and founder of learnhindituts.com. I live in Uttar Pradesh (UP), India and I love to talk about programming as well as writing technical tutorials and tips that can help to others.

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers