If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
पिछले topic में आपने continue statement के बारे में पढ़ा और समझा कि , किसी particular condition के लिए looping iteration को कैसे skip करें। लेकिन अगर हमें किसी condition के लिए loop को terminate करना हो तो ? वहां पर हम break का use करते हैं।
break का use for Loop , while Loop , do while Loop या switch loop के execution को terminate करता है।
#include <stdio.h>
int main() {
for(int num=1; num<10; num++) {
// terminate loop if num=5
if(num == 5) {
break;
}
printf("%d\n", num);
}
return 0;
}
1 2 3 4
Example में आप देख सकते हैं कि num की value 5 होते ही loop terminate हो गया।
break का use किसी loop के अंदर ही कर सकते हैं , normal statements या if else के साथ आप break use नहीं कर सकते हैं।
#include <stdio.h> int main() { int num = 4; if(num == 5) { break; } return 0; } Error : break statement not within loop or switch
ठीक इसी तरह से आप while Loop के साथ भी continue statement को use कर सकते हैं।
#include <stdio.h>
int main() {
int x = 0;
while(x < 10) {
x++;
// now check condition.
if(x==5) {
break;
}
printf("%d\n", x);
}
return 0;
}
1 2 3 4
ऊपर दिए गए example में हमें same patern print कराया है जो for loop में किया था , I hope आपको C language में break statement के बारे में समझ आया होगा।
Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)
Get connected with me. :) LinkedIn Twitter Instagram Facebook