Purchase Solution

Explaining why using break and continue is a bad practice

Not what you're looking for?

Ask Custom Question

This figure shows an example of using a command in Java called break and continue. I somewhat disagree with the use of these commands in this way. I consider it bad form as does much of the industry. Just because a command is there does not mean you have to use it!

Why this is bad form and provide a snippet of code that shows a better way to code the loops in this example.

I am actually using Jcreator to look at this code. You can either use Jcreator or Netbeans java program to do this one if you choose:
_______________________________________________________________________

// Fig. 5.12: BreakTest.java
// break statement exiting a for statement.
Public class BreakTest
{
Public static void main( String args[] )
{
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;

System.out.printf( "%d ", count );
} // end for

System.out.printf( "nBroke out of loop at count = %dn", count );
} // end main
} // end class BreakTest

Purchase this Solution

Solution Summary

This solution explains why using "break" and "continue" is a bad idea. This is illustrated using a small bit of Java code.

Solution Preview

The break and continue statements can be used in useful situations but many times they are abused. The code shown in this example is one of the "abuse" cases. Notice that the for loop will iterate up until the count is equal to 5. At that point the break statement will exit out of the loop. This can be ...

Purchase this Solution


Free BrainMass Quizzes
C# variables and classes

This quiz contains questions about C# classes and variables.

Excel Introductory Quiz

This quiz tests your knowledge of basics of MS-Excel.

Inserting and deleting in a linked list

This quiz tests your understanding of how to insert and delete elements in a linked list. Understanding of the use of linked lists, and the related performance aspects, is an important fundamental skill of computer science data structures.

Basic UNIX commands

Use this quiz to check your knowledge of a few common UNIX commands. The quiz covers some of the most essential UNIX commands and their basic usage. If you can pass this quiz then you are clearly on your way to becoming an effective UNIX command line user.

C++ Operators

This quiz tests a student's knowledge about C++ operators.