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);
Re: Why is break and continue bad to use?
Using break and continue is not a bad practice! it's just a bad example
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.