Read in Hindi
Read in English

Going forward with this C/C++ programming language tutorial today we will learn about two important keywords break and continue, which are needed in loops (for loop, while loop, switch case).

use of break in c/c++

In c/c++ to break a loop in the middle to come out of loop break statement is used. For example we want to search a number in an array. Using for loop we will go to each element of array and test if number is present or not. Here for loop will continue till we scan whole array. But if somewhere in middle we find desired number then we do not want to scan remaining elements of array. For this we will use break. Similarly break can be used to break while loop also.
Lets see the example below in which we want to search a number divisible by 21 between 100 and 200.
#include <stdio.h>

int main() { 
  int i;
  for(i=100; i<=200; i++) { 
    if(i%21 == 0) {
      printf("1st such number is %d\n", i);
      break;
    }
  }
  scanf("%d", &i);
  return 0;
}

Here we have if statement inside for loop. Remember that % gives remainder when dividing 1st number with 2nd. It will not go inside if for the numbers which gives i%21 does not give 0, in other words number is not divisible by 21. Hence in the beginning numbers are not divisible by 21, but as soon as we reach 105 it is divisible by 21 and it will go inside if and break will break the for loop. Hence finally for loop will run from 100 to 105 and because of break for loop will exit.
Similarly in c/c++ to break while loop break is used. We have seen use of break to break switch case statement.

use of continue in c/c++

As we know that loop is used to run some statements number of times. Some times without going to end statement work is done and we want to start again same loop. For this we use continue statement. To understand this lets see the example below. Using continue statement we will print only odd numbers in given array.
#include <stdio.h>
int main() {
  int arr[] = {1,4,7,2,0,-5,8,17,5,-10};
  int length = 10;
  int i;
  for(i=0; i<10; i++) {
    if(arr[i]%2 == 0) {
      continue;
    }
    printf("Odd number is %d\n", arr[i]);
  }
  scanf("%d", &i);
  return 0;
}

In the example given above using for loop we start reading number from given array. If number is even, we use continue statement to skip and go to next iteration of for loop.
Similarly continue can be used in while loop.
If you like this tutorial, share it with your friends on facebook, orkut, twitter etc.
आज Hindi के इस C/C++ programming language tutorial को आगे बढ़ाते हुए हम दो important keywords break और continue का use करना सीखेंगे, जो loops (for loop, while loop, switch case) में कभी कभी काम आ जाते हैं.

use of break in c/c++


c/c++ में किसी loop को बीच में ही खत्म करके उससे बाहर निकलने के लिए break का use किया जाता है. उदाहरण के लिए मान लीजिए एक int array में हम किसी number को खोजना चाहते हैं, इसके लिए for loop का use करके array के हर एक element को check करेंगे कि वह number वहां है कि नहीं. यहाँ for loop तब तक चलता रहेगा जब तक हम पूरी array check नहीं कर लेते, पर हम चाहते हैं कि बीच में जब भी वो number मिल जाये जिसे खोज रहे हैं तो for loop बंद करके loop से बाहर आ जाएँ. इसके लिए break का use करेंगे. for loop ही नहीं while loop से बीच में बाहर आने के लिए भी break जा use करते हैं.
इसे नीचे दिए गए example से समझते हैं. इसमें हम 100 से 200 के बीच पहली संख्या search करेंगे जो 21 से विभाजित हो जाये.
#include <stdio.h>

int main() { 
  int i;
  for(i=100; i<=200; i++) { 
    if(i%21 == 0) {
      printf("1st such number is %d\n", i);
      break;
    }
  }
  scanf("%d", &i);
  return 0;
}

यहाँ for loop के अंदर if statement है. आपको याद दिला दें कि % यह बताता है कि पहली संख्या में दूसरी का भाग देने पर शेष क्या बचेगा. जिन संख्याओ के लिए i%21 की value 0 नहीं है अर्थात वो 21 से विभाजित नहीं होते for loop में i के उन मानो लिए हम if के अंदर नहीं जायेंगे (ध्यान दे जैसा कि हम जानते हैं कि यहाँ for loop में i के 100 से 200 तक हर मान के लिए for loop के अंदर लिखे सारे statement run होंगे.) शुरू में if के अंदर वाले statement run नहीं होंगे पर जैसे ही i की value 105(21 से विभाजित) होगी if के अंदर चले जायेंगे और break run हो जायेगा और for loop खत्म हो जायेगा. इसलिए finally for loop के अंदर लिखे statement i के 100 से 105 तक की value के लिए ही run हो पाएंगे क्योंकि i=105 आने के बाद break run होने के कारण for loop खत्म हो जायेगा.
इसी तरह c/c++ में while loop को भी बीच में खत्म करने के लिए break का use करते हैं. switch case statement में हम break का use देख ही चुके हैं.

use of continue in c/c++

जैसा कि हम जानते हैं कि loop में (for loop, while loop) के कुछ statement बार बार run होते रहते हैं. कभी कभी loop के अंत तक जाये बिना ही हमारा काम हो जाता है और हम चाहते हैं कि loop बंद न हो पर इस बार loop के अंदर जो run हो रहा है वो यहीं बंद हो जाये और loop का अगला iteration start हो जाये, इसके लिए continue का use करते हैं. इसे भी नीचे दिए गए example से समझते हैं. इस example में एक array में कुछ number दिए गए हैं. हम continue का use करके odd numbers (विषम संख्याए) print करेंगे.
#include <stdio.h>
int main() {
  int arr[] = {1,4,7,2,0,-5,8,17,5,-10};
  int length = 10;
  int i;
  for(i=0; i<10; i++) {
    if(arr[i]%2 == 0) {
      continue;
    }
    printf("Odd number is %d\n", arr[i]);
  }
  scanf("%d", &i);
  return 0;
}

ऊपर दिए गए example में for loop का use करके शुरू से array के एक एक number को देखते हैं. अगर number even है तो हम continue का use करके skip कर देते हैं और अगली बार for loop के अन्दर आते हैं.
इसी तरह while loop में भी continue का use कर सकते हैं.
इस blog को बेहतर बनाने के लिए आपके सुझावों का सदैव स्वागत रहेगा. सुझाव देने के लिए अपना Reply यहाँ पर दें. अगले लेख में हम function के बारे में जानेंगे जो कि बहुत important है.