break Statement in C Language with Examples

What is break Statement in C?

In C programming language, the break statement is a jump statement used to terminate a loop or a switch statement immediately. It transfers program control to the statement immediately following the loop or switch statement. The break statement is mainly used to perform the following two tasks:

C प्रोग्रामिंग भाषा में break statement एक jump statement है, जिसका उपयोग किसी loop या switch statement को तुरंत समाप्त करने के लिए किया जाता है। यह program control को loop या switch statement के तुरंत बाद लिखे गए अगले statement पर transfer कर देता है। break statement का मुख्य रूप से निम्नलिखित दो कार्यों के लिए उपयोग किया जाता है:

1. Terminating a Loop

When a break statement is executed inside a loop (for, while, or do-while), the loop is immediately terminated, and program control moves to the next statement after that loop.

जब break statement का उपयोग किसी loop (for, while या do-while) के अंदर किया जाता है, तो यह loop को तुरंत समाप्त कर देता है और program control loop के बाद लिखे गए अगले statement पर चला जाता है।

2. Terminating a switch Statement

When a break statement is executed inside a switch statement, it terminates the entire switch statement and transfers control to the statement following the switch. If break is omitted, execution continues into the subsequent cases. This behavior is called fall-through.

जब break statement का उपयोग switch statement के अंदर किया जाता है, तो यह पूरे switch statement को समाप्त कर देता है और control switch के बाद लिखे गए अगले statement पर चला जाता है। यदि break का उपयोग नहीं किया जाता है, तो execution आगे लिखे गए cases में भी जारी रहता है। इस व्यवहार को Fall-through कहते हैं।

Syntax of break Statement

break;

The break statement is written with a semicolon. It can be used inside loops and switch statements.

break statement के अंत में semicolon लगाया जाता है। इसका उपयोग loops और switch statements के अंदर किया जा सकता है।

Example 1: break Statement in for Loop

Program to terminate a for loop when the value of i becomes 5.

#include <stdio.h>
int main(void)
{
int i;
for(i = 1; i <= 10; i++)
{
if(i == 5)
{
break;
}
printf("%d ", i);
}
printf("\nLoop terminated.");
return 0;
}

Output:

1 2 3 4
Loop terminated.

Explanation: The loop starts from 1 and continues up to 10. When the value of i becomes 5, the condition i == 5 becomes true and the break statement terminates the loop immediately. Therefore, 5 to 10 are not printed.

Loop की शुरुआत 1 से होती है और यह 10 तक चलता है। जब i का मान 5 हो जाता है, तब i == 5 condition true हो जाती है और break statement loop को तुरंत समाप्त कर देता है। इसलिए 5 से 10 तक की संख्याएँ print नहीं होती हैं।

Example 2: break Statement in while Loop

Program to terminate a while loop when the value of i becomes 4.

#include <stdio.h>
int main(void)
{
int i = 1;
while(i <= 10)
{
if(i == 4)
{
break;
}
printf("%d ", i);
i++;
}
printf("\nLoop terminated.");
return 0;
}

Output:

1 2 3
Loop terminated.

Explanation: The while loop continues as long as i is less than or equal to 10. When i becomes 4, the break statement terminates the loop.

व्याख्या: While loop तब तक execute होता है जब तक i का मान 10 से कम या बराबर रहता है। जब i का मान 4 हो जाता है, तब break statement loop को समाप्त कर देता है।

Example 3: break Statement in switch Case

Program to demonstrate the use of break statement in a switch case.

#include <stdio.h>
int main(void)
{
int choice = 2;
switch(choice)
{
case 1:
printf("One");
break;
case 2:
printf("Two");
break;
case 3:
printf("Three");
break;
default:
printf("Invalid Choice");
}
printf("\nSwitch statement terminated.");
return 0;
}

Output:

Two
Switch statement terminated.

Explanation: The value of choice is 2, so case 2 is executed. After printing "Two", the break statement terminates the entire switch statement. Control then moves to the next statement after the switch.

व्याख्या: choice का मान 2 है, इसलिए case 2 execute होता है। "Two" print होने के बाद break statement पूरे switch statement को समाप्त कर देता है। इसके बाद control switch के अगले statement पर चला जाता है।

Example 4: switch Case Without break Statement

Program to demonstrate fall-through in a switch statement.

#include <stdio.h>
int main(void)
{
int choice = 2;
switch(choice)
{
case 1:
printf("One\n");
case 2:
printf("Two\n");
case 3:
printf("Three\n");
break;
default:
printf("Invalid Choice");
}
return 0;
}

Output:

Two
Three

Explanation: Since the value of choice is 2, execution starts from case 2. As there is no break after case 2, execution continues into case 3. This is called fall-through. The break statement after case 3 terminates the switch statement.

व्याख्या: क्योंकि choice का मान 2 है, इसलिए execution case 2 से शुरू होता है। Case 2 के बाद break नहीं होने के कारण execution case 3 में भी चला जाता है। इसे Fall-through कहते हैं। Case 3 के बाद लिखा गया break पूरे switch statement को समाप्त कर देता है।

Important Points About break Statement

  1. The break statement is used to terminate loops and switch statements.

  2. It can be used inside for, while, and do-while loops.

  3. When executed inside a loop, it terminates only the innermost enclosing loop.

  4. When executed inside a switch statement, it terminates the entire switch statement.

  5. The break statement is optional in a switch case, but omitting it may cause fall-through.

  6. The break statement cannot be used independently outside a loop or switch statement.

  7. break statement का उपयोग loops और switch statements को समाप्त करने के लिए किया जाता है।

  8. इसका उपयोग for, while और do-while loops के अंदर किया जा सकता है।

  9. Loop के अंदर execute होने पर यह केवल सबसे अंदर वाले संबंधित loop को समाप्त करता है।

  10. Switch statement के अंदर execute होने पर यह पूरे switch statement को समाप्त करता है।

  11. Switch case में break statement optional होता है, लेकिन इसे न लिखने पर Fall-through हो सकता है।

  12. break statement का उपयोग loop या switch statement के बाहर स्वतंत्र रूप से नहीं किया जा सकता।

The break statement is an important control statement in C programming. It provides a way to terminate loops and switch statements before their normal completion. It is especially useful when a particular condition is satisfied and further execution is not required.

C programming में break statement एक महत्वपूर्ण control statement है। यह loops और switch statements को सामान्य रूप से समाप्त होने से पहले ही terminate करने की सुविधा देता है। जब कोई विशेष condition पूरी हो जाती है और आगे execution की आवश्यकता नहीं होती, तब इसका उपयोग बहुत उपयोगी होता है।

Comments

Popular posts from this blog

C Language Topics in Hindi and English

Complete C Programming Syllabus | Advanced C Roadmap for Students

Top High-Paying Tech Skills to Learn in 2025