Sunday, May 28, 2017
break control statement
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 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 ".
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 doesnt 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.
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.
Available link for download
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment