Nested Loops with break and continue Statements in C
In C programming, nested loops are used when one loop is written inside another loop. We can also use break and continue statements inside nested loops to control their execution. When break or continue is used inside an inner loop, it affects only the innermost enclosing loop. The outer loop continues its execution normally unless it is also terminated by a separate control statement.
C programming में जब एक loop के अंदर दूसरा loop लिखा जाता है, तब उसे Nested Loop कहते हैं। Nested loops के execution को control करने के लिए हम break और continue statements का भी उपयोग कर सकते हैं। जब break या continue statement का उपयोग inner loop के अंदर किया जाता है, तब यह केवल उसी सबसे अंदर वाले loop को प्रभावित करता है। Outer loop सामान्य रूप से execute होता रहता है, जब तक कि उसे अलग से terminate न किया जाए।
1. break Statement in Nested Loops
When a break statement is executed inside an inner loop, it immediately terminates only the inner loop. After terminating the inner loop, program control moves to the next statement following that inner loop, and the outer loop continues with its next iteration.
जब inner loop के अंदर break statement execute होता है, तब यह केवल inner loop को तुरंत समाप्त करता है। Inner loop समाप्त होने के बाद program control उसके अगले statement पर जाता है और outer loop अपना अगला iteration execute करता है।
Example 1: break Statement in Nested for Loop
Program to demonstrate the use of break statement in nested loops.
#include <stdio.h>int main(void){int i, j;for(i = 1; i <= 3; i++){for(j = 1; j <= 5; j++){if(j == 3){break;}printf("%d ", j);}printf("\n");}return 0;}
Output:
1 21 21 2
Explanation: The outer loop executes three times. The inner loop starts from 1 in every iteration of the outer loop. When j becomes 3, the break statement terminates the inner loop. Therefore, only 1 and 2 are printed in each row. The outer loop continues until its condition becomes false.
व्याख्या: Outer loop तीन बार execute होता है। Outer loop के प्रत्येक iteration में inner loop 1 से शुरू होता है। जब j का मान 3 होता है, तब break statement inner loop को समाप्त कर देता है। इसलिए प्रत्येक row में केवल 1 और 2 print होते हैं। Outer loop अपनी condition false होने तक चलता रहता है।
Example 2: Terminating Both Loops Using break
A normal break statement terminates only the innermost enclosing loop. If we want to terminate both inner and outer loops, we can use an additional condition or another control mechanism.
सामान्य break statement केवल सबसे अंदर वाले loop को terminate करता है। यदि हमें inner और outer दोनों loops को terminate करना है, तो हम अतिरिक्त condition या किसी अन्य control mechanism का उपयोग कर सकते हैं।
Program to terminate both loops using a flag variable.
#include <stdio.h>int main(void){int i, j;int stop = 0;for(i = 1; i <= 3 && !stop; i++){for(j = 1; j <= 5; j++){if(i == 2 && j == 3){stop = 1;break;}printf("%d %d\n", i, j);}}return 0;}
Output:
1 11 21 31 41 52 12 2
Explanation: When i becomes 2 and j becomes 3, the flag variable stop is set to 1 and break terminates the inner loop. The outer loop condition then becomes false, so both loops stop.
व्याख्या: जब i का मान 2 और j का मान 3 होता है, तब flag variable stop का मान 1 कर दिया जाता है और break inner loop को समाप्त कर देता है। इसके बाद outer loop की condition false हो जाती है, इसलिए दोनों loops रुक जाते हैं।
2. continue Statement in Nested Loops
When a continue statement is executed inside an inner loop, it skips the remaining statements of the current inner-loop iteration and transfers control to the next iteration of that inner loop. The outer loop is not affected.
जब inner loop के अंदर continue statement execute होता है, तब यह inner loop के वर्तमान iteration के बाकी statements को skip कर देता है और control को inner loop के अगले iteration पर भेज देता है। इससे outer loop प्रभावित नहीं होता है।
Example 3: continue Statement in Nested for Loop
Program to print numbers from 1 to 5 in three rows, skipping number 3 in every row.
#include <stdio.h>int main(void){int i, j;for(i = 1; i <= 3; i++){for(j = 1; j <= 5; j++){if(j == 3){continue;}printf("%d ", j);}printf("\n");}return 0;}
Output:
1 2 4 51 2 4 51 2 4 5
Explanation: The outer loop executes three times. Whenever the inner loop variable j becomes 3, the continue statement skips the printf() statement for that iteration. The inner loop then continues with the next value of j.
व्याख्या: Outer loop तीन बार execute होता है। जब भी inner loop के variable j का मान 3 होता है, तब continue statement उस iteration के लिए printf() statement को skip कर देता है। इसके बाद inner loop j की अगली value के साथ execute होता रहता है।
Example 4: Using break and continue Together in Nested Loops
Program to demonstrate both break and continue statements in nested loops.
#include <stdio.h>int main(void){int i, j;for(i = 1; i <= 3; i++){for(j = 1; j <= 5; j++){if(j == 2){continue;}if(j == 5){break;}printf("%d ", j);}printf("\n");}return 0;}
Output:
1 3 41 3 41 3 4
Explanation: When j becomes 2, continue skips the current iteration. When j becomes 5, break terminates the inner loop. The outer loop then continues with its next iteration. This process repeats three times.
व्याख्या: जब j का मान 2 होता है, तब continue वर्तमान iteration को skip कर देता है। जब j का मान 5 होता है, तब break inner loop को समाप्त कर देता है। इसके बाद outer loop अपना अगला iteration execute करता है। यह प्रक्रिया तीन बार दोहराई जाती है।
Difference Between break and continue in Nested Loops
| break Statement | continue Statement |
|---|---|
| Terminates the innermost enclosing loop. | Skips the current iteration of the innermost enclosing loop. |
| Control moves outside the terminated loop. | Control moves to the next iteration of the loop. |
| The remaining iterations of that loop are not executed. | The remaining iterations of that loop continue normally. |
| It does not automatically terminate the outer loop. | It does not affect the outer loop. |
break Statement | continue Statement |
|---|---|
| यह सबसे अंदर वाले संबंधित loop को समाप्त करता है। | यह सबसे अंदर वाले संबंधित loop के वर्तमान iteration को skip करता है। |
| Control समाप्त हुए loop के बाहर चला जाता है। | Control loop के अगले iteration पर चला जाता है। |
| उस loop के बाकी iterations execute नहीं होते हैं। | उस loop के बाकी iterations सामान्य रूप से execute होते रहते हैं। |
| यह अपने आप outer loop को terminate नहीं करता है। | यह outer loop को प्रभावित नहीं करता है। |
Important Points
Both
breakandcontinuecan be used inside nested loops.A
breakstatement terminates only the innermost enclosing loop.A
continuestatement skips only the current iteration of the innermost enclosing loop.The outer loop continues its execution normally.
To terminate multiple nested loops, we can use a flag variable, an additional condition, or a function return where appropriate.
In nested
forloops,continuemoves control to the update expression of the innermost loop.In nested
whileanddo-whileloops,continuemoves control to the condition check of the innermost loop.Nested loops के अंदर
breakऔरcontinueदोनों statements का उपयोग किया जा सकता है।breakstatement केवल सबसे अंदर वाले संबंधित loop को समाप्त करता है।continuestatement केवल सबसे अंदर वाले संबंधित loop के वर्तमान iteration को skip करता है।Outer loop सामान्य रूप से execute होता रहता है।
एक से अधिक nested loops को terminate करने के लिए flag variable, अतिरिक्त condition या आवश्यकता के अनुसार function में
returnका उपयोग किया जा सकता है।Nested
forloops मेंcontinuecontrol को सबसे अंदर वाले loop के update expression पर भेजता है।Nested
whileऔरdo-whileloops मेंcontinuecontrol को सबसे अंदर वाले loop की condition check पर भेजता है।
The break and continue statements are useful for controlling the execution of nested loops in C programming. The break statement terminates the innermost loop, whereas the continue statement skips the current iteration of that loop. Understanding their behavior makes it easier to write programs involving patterns, tables, matrices, and other nested-loop operations.
C programming में nested loops के execution को control करने के लिए break और continue statements उपयोगी हैं। break statement सबसे अंदर वाले loop को समाप्त करता है, जबकि continue statement उस loop के वर्तमान iteration को skip करता है। इनके working को समझने से patterns, tables, matrices और अन्य nested-loop programs लिखना आसान हो जाता है।
Comments
Post a Comment