Section 10k

Bad Practice - Using break and continue


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Other Decision Making Management Tools

Don't Use These

As you have seen previously, there are parts of the language that lead to poor programming, such as being able to declare variables other than at the beginning of the method or decision-making statements that don't require curly braces. Every language has its good and bad points, and Java, being one of the most pervasively used programming languages, has its share. That said, there are only two really bad ones that you will see on the Internet (and other poor programming sources), and these are provided here.

Using the break Keyword for Loops (pretty bad)

One keyword which you have already used is the break keyword. In your previous use, break allowed you to conduct switch/case actions, and they "broke" you out of the rest of the process. That is exactly how the keyword is used in program repetition. Consider the following code.

// in global constant area of program
private final char PERIOD = '.';

// in method area where loop is (local)
int counter, letterCounter = 0;

// prompt for input string
// method: printString
conIO.printString( "Enter string ended by a period: " );

// loop up to max sentence length
for( counter = 1; counter <= MAX_SENTENCE_LENGTH; counter++ )
{
// get the next character
// method: getChar
nextChar = conIO.getChar();

// check for period
if( nextChar == PERIOD )
{
// stop loop
break;
}

// otherwise, assume not a period
else
{
// increment the letter counter
letterCounter++;
}
}
// end loop

This loop would be stopped, and no further letters would be counted once the input process got to the period of a sentence. There are several better alternatives to this code that represent better quality application of logic, but here is one.

// in global constant area of program
private final char PERIOD = '.';

// in method area where loop is (local)
int counter, letterCounter = 0;

// prompt for input string
// method: printString
conIO.printString( "Enter string ended by a period: " );

// loop up to max sentence length
for( counter = 1;
counter <= MAX_SENTENCE_LENGTH
&& nextChar != PERIOD; counter++ )
{
// get the next character
// method: getChar
nextChar = conIO.getChar();

// check for period
if( nextChar != PERIOD )
{
// increment the letter counter
letterCounter++;
}
}
// end loop

In some programs, the following kind of loop is used.

// in method area where loop is (local)
double testValue, testSum = 0.0;

// loop forever
while( true )
{
// display user prompt
// method: promptForInt
testValue = conIO.promptForDouble( "Enter next test score: " );

// check for negative value
if( testValue < 0.00 )
{
// stop loop
break;
}

// otherwise, assume good value
else
{
// add value to sum
testSum += testValue;
}
}
// end loop

The above loop would continue prompting the user for data entry until the user entered a negative value. As mentioned above, you really do not need to use the break keyword to accomplish this because a do . . while loop would work just fine here, and would be more logical and readable. Here is an example.

Also note that without the break statement, this is an infinite loop due to the while( true ) code. Creating infinite loop conditions like this is also done in some circumstances, but in almost every case, it is clear evidence that the person writing the code could not come up with a logical condition that would have worked better (and in most cases, it's not that hard to do), like the following:

// in method area where loop is (local)
double testValue, testSum = 0.0;

// start loop
do
{
// display user prompt
// method: printString
testValue = conIO.promptForDouble( "Enter test score: " );

// check for negative value
if( testValue >= 0.00 )
{
// add value to sum
testSum += testValue;
}
}
while( testValue >= 0.0 );
// end loop, if test value negative

Here is an even better approach, using your loop priming strategy.

// in method area where loop is (local)
double testValue, testSum = 0.0;

// prime input with user prompt and input
// method: promptForInt
testValue = conIO.promptForDouble( "Enter first test score: " );

// loop until negative number is found
while( testValue >= 0.0 )
{
// add value to sum
testSum += testValue;

// display user prompt
// method: promptForInt
testValue = conIO.promptForDouble( "Enter next test score: " );
}
// end loop

In almost every case, it shows better quality, and many times more clear, program logic when you write your loop code without the use of break. As you know by now, there are so many different ways to manage logical conditions; why not use one that demonstrates better quality? However, some programs manage their loops this way, and it is a legal way to do so. Again however, it is not evidence of good quality program analysis, and is very poor programming.

Later in the text, when you are implementing searching and sorting processes, you may find that the break keyword can be helpful IF you cannot come up with better or more readable logic. Still, you should exhaust your ideas before using the break keyword.

Using the continue Keyword (really bad)

Another keyword that can be used in loops is the continue keyword. Know this: use of the continue keyword in code is clear evidence that the programmer simply does not understand good logic. This is another case where its use is legal, but it is simply not acceptable code.

Here is an example.

// in method area where loop is (local)
double testValue, testSum = 0.0;

// user prompt - prime
// method: promptForDouble
testValue = conIO.promptForDouble( "Enter first test score: " );

// loop while input value greater than zero
while( testValue > 0.0 )
{
// check for input value greater than 100 which would be bad data
if( testValue > 100.00 )
{
// prompt user for next item
// method: promptForDouble
testValue = conIO.promptForDouble( "Enter next test score: " );

// start loop over
continue;
}

// assume good value and add to sum
testSum += testValue;

// reprompt user for next item
// method: promptForDouble
testValue = conIO.promptForDouble( "Enter next test score: " );
}

The code provided here shows good use of priming and loop control, and incorporates a test in case a value is entered that is larger than a test score should be. If a value greater than 100.00 is entered, the loop will skip over any further code in the loop, but unlike the break operation, it will continue the loop rather than shutting it down.

Again while this is legal, it is very poor programming development. You should be able to look at the code and quickly identify at least two or three ways to do this in a more readable, quality way, without the use of the continue keyword. Here is one of many possible examples.

// in method area where loop is (local)
double testValue, testSum = 0.0;

// user prompt - prime
// method: promptForDouble
testValue = conIO.promptForDouble( "Enter first test score: " );

// loop while input value greater than zero
while( testValue > 0.0 )
{
// check for input value less than or equal to 100
if( testValue <= 100.00 )
{
// add tested number to sum
testSum += testValue;
}

// reprompt user for next item
// method: promptForDouble
testValue = conIO.promptForDouble( "Enter next test score: " );
}

Don't use continue; it would be embarrassing.

The Process of Repetition, Enhanced

This is nearly the end of your program repetition learning. There is one more component called recursion that will fill out your knowledge of ways to iterate in a program. As you finish up this set of topics, you are gaining practice and experience in all the areas of programming.