Source One Educational Society    
Home | OS Install | MS-Office | Photoshop | SQL | Networking | HTML | Java | Model Papers | FAQ's | Health Tips | News | Jobs | Fun SMS
C - Tutorial
INTRODUCTION
o Getting Started
o C Boot Disk
o What Is An Identifier
o Getting Started C
PROGRAM CONTROL
o While Loop
o Do-While Loop
o For Loop
o If Statement
o Break And Continue
o Switch Statement
o Goto Statement
o Finally, A Meaningful Program
ASSIGNMENT & LOGICAL COMPARES
o Integer Assignment Statements
o Additional Data Types
   
   
 
 
C Language Tutorial
Program Control  

The Break And Continue

Load the file named breakcon.c for an example of two new statements.

main( )
{
int xx;
for(xx = 5;xx < 15;xx = xx + 1){
if (xx == 8)
break;
printf("in the break loop, xx is now %d\n",xx);
}
for(xx = 5;xx < 15;xx = xx + 1){
if (xx == 8)
continue;
printf("In the continue loop, xx is the now %d\n",xx);
}
}

Notice that in the first "for" there is an if statement that calls a break if xx equals 8. The break will jump out of the loop you are in and begin executing the statements following the loop, effectively terminating the loop. This is a valuable statement when you need to jump out of a loop depending on the value of some results calculated in the loop. In this case, when xx reaches 8, the loop is terminated and the last value printed will be the previous value, namely 7.

The next "for" loop, contains a continue statement which does not cause termination of the loop but jumps out of the present iteration. When the value of xx reaches 8 in this case, the program will jump to the end of the loop and continue executing the loop, effectively eliminating the printf statement during the pass through the loop when xx is eight. Compile and run the program to see if it does what you expect.

 
 
 
Quick Links
Java FAQ's
XML FAQ's
.Net FAQ's
Javascript FAQ's
SQL Interview Questions
 
 
 
 
Home  |  Aboutus  |  Post Your Content  |  Tell a Friend  |  Contact us  |  Sitemap
© 2009 365edu. All Rights Reserved. Site by 365edu. Best view in 1024x768 resolution.