JS Ext

Monday, February 18, 2013

Why I don't use 'break'

I have been looking at a lot of code lately.  Some of it is c++ while most of it is Java.  People seem to like using the 'break' keyword in code.  I never really liked it because I feel like it is ambiguous.  What am I breaking?  Is it the nearest switch statement?  Is it the nearest for loop?  How can I intuitively read code if I'm not sure which control statement is being broken?  Imagine the following code.  What does it print out?


for ( int i = 0; i < 4; i++ )
{
 for ( int j = 0; j < 4; j++ )
 {
  System.out.println( i + " " + j );
  if ( i == 2 && j == 1 )
  {
   break;
  }
 }
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.