break statement is used to control the transfer out the block without executing the statements written after break statement . It is used either with switch or looping statement.

Syntax of break statement :
break ;  or   break  < label > ;

< label > : It is used with looping statement and with break it indicates which loop execution will be terminated in the current nesting block.

Some of the Invalid statement of the break statement gives compilation error as " unreachable statement "

for ( int i = 0 ; i < = 10 ; i ++ ) {
System.out.println ( " Before break ") ;
break ;
System.out.println (" After break " );   // Compilation error " unreachable statement "
}

while(true) {
break;
System.out.println("After break") ;   // Compilation error " unreachable statement "
}

In the above code compiler is very sure that statement written after break statement will never execute and hence give compilation error as " unreachable statement ".

Above code with some modification which doesn't give any compilation error.

for ( int i = 0 ; i < = 10 ; i ++ ) {
if ( i == 1 )
break ;
System.out.println (" After break " );   
}

In the above code if statement is used and break is used with it, which indicates that there may be chance of executing statements written after break , when if condition will be false. 

while(true) {
int  i = 10 ;
if ( i == 1 )
break;
System.out.println("After break") ;  
i-- ;
}

Above code will not give any error but execute infinitely because " i " is a local variable and it always allocate memory when loop execute. So its value never fall down from 10 .

Use of break with label 

Label can be used with any looping statement i.e for and while loop.for ex-

ab:                                                       // Here ab is a label for " for " loop.
for ( int i = 0 ; i < = 10 ; i ++ ) { }

ab1 :
for ( int i = 0 ; i < = 10 ; i ++ ) { 
ab2 :
for ( int j = 0 ; j < = 10 ; j ++ ) { 
ab3 :
for ( int k = 0 ; k < = 10 ; k ++ ) { 
break ;                                          // Terminate the current block.
break ab3 ;                                  //  Terminate the k block.
break ab2 ;                                 //  Terminate the j block.
break ab1 ;                                //  Terminate the i block.
}

Note: Let suppose we have multiple array with some value and we want to find some value i.e 34 is available or not  then we need to go for nested loop.  If we found 34 in any one array then no need to execute loop further so we can use break with label statement to transfer the control out of all the loop. 

Related Posts:

  • If Statement in Java As we know that " if " is a conditional control statement and maximum it will execute once depending upon the condition check. If the condition is true then it will execute once and if false then will not execute at even fo… Read More
  • Switch control statement In programming languages switch is a type of conditional control statement. It has one switch statement and many cases with one default statement. Depending upon the switch value it matches with the cases value and execute … Read More
  • " for " Looping statement Any looping statement is comprises of there stages i.e 1> Initialization                   2> Conditional                   &n… Read More
  • " break " control statement break statement is used to control the transfer out the block without executing the statements written after break statement . It is used either with switch or looping statement. Syntax of break statement : break ;  or… Read More
  • " while " looping statement " while " statement is also used as the looping statement in any programming languages. It only works with the condition and we can use updation statement inside the block otherwise it will go for infinite loop if the condit… Read More

0 comments:

Post a Comment

Blog Archive

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

201976

Online Members

Live Traffic