C Programs to demonstrate use of break Statement in All Three types of Loops (for, while and do while loop)
The break statement terminates a loop immediately when the specified condition becomes true.
C में break statement condition true होने पर loop को तुरंत terminate कर देता है।
Syntax
break;1. break Statement in for Loop
C Program
#include <stdio.h>
int main()
{
int i;
printf("Counting from 1 to 10\n");
for (i = 1; i <= 10; i++)
{
printf("%d\n", i);
if (i == 7)
break;
}
return 0;
}Output
Counting from 1 to 10
1
2
3
4
5
6
7Effect: When i becomes 7, break terminates the for loop. Numbers 8, 9, and 10 are not printed.
जब i की value 7 होती है, तो break for loop को terminate कर देता है। 8, 9 और 10 print नहीं होते।
2. break Statement in while Loop
C Program
#include <stdio.h>
int main()
{
int i = 1;
printf("Counting from 1 to 10\n");
while (i <= 10)
{
printf("%d\n", i);
if (i == 7)
break;
i++;
}
return 0;
}Output
Counting from 1 to 10
1
2
3
4
5
6
7Effect: When i becomes 7, the break statement terminates the while loop.
जब i की value 7 होती है, तो break statement while loop को terminate कर देता है।
3. break Statement in do-while Loop
C Program
#include <stdio.h>
int main()
{
int i = 1;
printf("Counting from 1 to 10\n");
do
{
printf("%d\n", i);
if (i == 7)
break;
i++;
}
while (i <= 10);
return 0;
}Output
Counting from 1 to 10
1
2
3
4
5
6
7Effect: When i becomes 7, the break statement terminates the do-while loop without checking the loop condition again.
जब i की value 7 होती है, तो break do-while loop को terminate कर देता है और loop condition दोबारा check नहीं होती।
Conclusion
The break statement works similarly in all three loops (for, while, and do-while). It immediately terminates the loop and transfers control to the statement following the loop.
break statement तीनों loops में समान रूप से कार्य करता है। यह loop को तुरंत terminate करके control को loop के बाद वाले statement पर भेज देता है।
Comments
Post a Comment