Search

Friday, December 21, 2018

Statements break and continue

The break and continue statements alter the flow of control. The break statement, when executed in a while, for, do/while or switch structure, causes immediate exit from that structure. Execution continues with the first statement after the structure. Common uses of the break statement are to escape early from a loop or skip the remainder of a switch structure . Figure demonstrates the break statement in a for repetition structure. 

 // Using the break statement in a for structure 
 // Java extension packages  
import javax.swing.JOptionPane;
public class BreakTest 
{
 // main method begins execution of Java application
public static void main( String args[] )
{
String output = "";
int count;
 // loop 10 times
for ( count = 1; count <= 10; count++ )
{
// if count is 5, terminate loop         
if ( count == 5 ) 
break;   // break loop only if count == 5     
 output += count + " "; 
 }  // end for structure
 output += "\nBroke out of loop at count = " + count;
 JOptionPane.showMessageDialog( null, output );
System.exit( 0 ); // terminate application 
// end method main 
} // end class BreakTest

When the if structure at line 19 in the for structure detects that count is 5, the break statement at line 20 executes. This statement terminates the for structure, and the program proceeds to line 26 (immediately after the for). Line 26 completes the string to display in a message dialog at line 27. The loop fully executes its body only four times. The continue statement, when executed in a while, for or do/while structure, skips the remaining statements in the loop body and proceeds with the next iteration of the loop. In while and do/while structures, the program evaluates the loop-continuation test immediately after the continue statement executes. In for structures, the increment expression executes, then the program evaluates the loop-continuation test. Earlier, we stated that the while structure could be used in most cases to represent the for structure. The one exception occurs when the increment expression in the while structure follows the continue statement. In this case, the increment does not execute before the program evaluates the repetition-continuation condition, so the while structure does not execute in the same manner as does the for structure. Figure uses the continue statement in a for structure to skip the string concatenation statement (line 22) when the if structure (line 18) determines that the value of count is 5. When the continue statement executes, program control continues with the increment of the control variable in the for structure. 

// Java extension packages
import javax.swing.JOptionPane;
public class ContinueTest 
{
 // main method begins execution of Java application 
public static void main( String args[] )
{
String output = ""; 
 // loop 10 times 
for ( int count = 1; count <= 10; count++ )
{
// if count is 5, continue with next iteration of loop
if ( count == 5 )
continue;
 // skip remaining code in loop  
// only if count == 5 
output += count + " ";
// end for structure  
output += "\nUsed continue to skip printing 5";
JOptionPane.showMessageDialog( null, output );
System.exit( 0 ); // terminate application
}
 // end method main 
}  
// end class ContinueTest


0 comments:

Post a Comment