Results 1 to 3 of 3

Thread: Why is break and continue bad to use?

  1. #1

    Thread Starter
    Addicted Member dim_kevin_as_human's Avatar
    Join Date
    Oct 2005
    Location
    Wisconsin
    Posts
    183

    Why is break and continue bad to use?

    For example, in Java used in a loop? Why is this a bad practice or bad form? Example:

    Code:
          int count; // control variable also used after loop terminates
          
          for ( count = 1; count <= 10; count++ ) // loop 10 times
          {  
             if ( count == 5 ) // if count is 5, 
                break;         // terminate loop
    
             System.out.printf( "%d ", count );
          } // end for
    
          System.out.printf( "\nBroke out of loop at count = %d\n", count );
    what would be the best way to do this? Like this?

    Code:
                      int count; // control variable also used after loop terminates
    
            for (count = 1; count <= 4; count++) // loop 5 times
            {
                System.out.printf("%d ", count);
            } // end for
    
            System.out.printf("\nBroke out of loop at count = %d\n", count);
    Dreaming men are haunted men.

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Why is break and continue bad to use?

    Using break and continue is not a bad practice! it's just a bad example
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3
    Hyperactive Member BramVandenbon's Avatar
    Join Date
    Jan 2002
    Location
    Belgium
    Posts
    502

    Re: Why is break and continue bad to use?

    I must agree with ComputerJy.

    There's no point in using a for-condition like <=10 if you will always break at ==5.
    Then of course it's better to use <=4 in your for-condition.

    But there are a lot of other situations where break; is fine. But this example has nothing to do with it.
    ____________________________________________

    Please rate my messages. Thank you!
    ____________________________________________
    Bram Vandenbon
    http://www.bramvandenbon.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width