
Utilizing Break and Continue Statements in Loops
Learn how to efficiently manage loops in programming using break statements to exit loops prematurely and continue statements to skip parts of the loop body. Explore examples and discover how these statements can be used in different scenarios.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
The break Statement Used in order to immediately exit from a loop After a break, following statements in the loop body are skipped and execution continues with the first statement after the loop If a break is executed from within nested loops, only the innermost loop is terminated 6/18/2025 1
Exiting a loop with break statement while ( .) do { . { . If(condition) If(condition) break; break; Exit Exit From From loop loop Exit Exit From From loop loop . . } // end of while } while( ); //next .. // next statem stateme ent nt 6/18/2025 2
Exiting a loop with break statement for ( .) for ( .) { . . for( ..) for( ..) { If(condition) If(condition) break; break; stmts stmts of inner loop; of inner loop; }// inner for loop ends // inner for loop ends . .stmts stmts of outer loop; of outer loop; } // outer for loop ends // outer for loop ends next next Stmts Stmts; ; for for { . . If(condition) If(condition) break; break; Exit Exit From From loop loop Exit Exit From From inner inner loop loop . . } next next Stmts Stmts; ; 6/18/2025 3
Check whether given number is prime or not int j, prime=1; scanf( %d ,&N); for( int j=2; j<N; j++ ) { if( (N % j) == 0) { prime=0; break; /* break out of for loop */ } } if (prime == 1) printf( %d is a prime no ,N); else printf( %d is a not a prime no ,N); 6/18/2025 4
Program to generate prime numbers between given 2 limits scanf( %d %d ,&m,&n); for( int i=m; i<=n; i++) { int prime=1; for( int j=2; j<i; j++ ) { if( i % j == 0) { } } if (prime == 1) printf( %d\t ,i); } prime=0; break; /* break out of inner loop */ 5 6/18/2025
Skipping a part of loop-continue statement Skip a part of the body of the loop under certain conditions is done using continue statement. As the name implies, continue causes the loop to be continued with next iteration, after skipping rest of the body of the loop. do while ( .) { Statement-1; Statement-2; { Statement-1; Statement-2; If(condition) continue; If(condition) continue; Statement-3; Statement-4; } while( ); Statement-3; Statement-4; } Next_statement Next_statement 6/18/2025 6
Skipping a part of loop - example for(i=10; i<=15; i++ ) { if(i==13 || i==14) printf( %d\t ,i); } continue; 10 11 12 15 7 6/18/2025
Skipping a part of loop for ( for ( i = 1 ; i <= 2 ; i++ ) ) { for ( for ( j = 1 ; j <= 2 ; j++ ) ) { if ( if ( i == j ) ) continue ; continue ; printf( printf( \ \n %d } } 1 2 2 1 n %d\ \t %d t %d\ \n n ,i, j); ; 8 6/18/2025