Much A-do About Nothing

I am currently revising my “Big Java” book, a college text for beginning computer science students. In the chapter on loops, I cover the while loop and the for loop extensively, and then there is an “advanced topic” that briefly introduces the do-while loop.

Why am I not keen on bigger coverage of the do-while loop? First, do-while loops are quite rare in practice. Using a few grep statements, I get these statistics for the code that is distributed with JDK 6 in src.zip:

for 7297 71%
while 2768 27%
do-while 237 2%

Here are the grep statements in case someone wants to improve on this:

grep -r -h "[^A-Za-z0-9]for (" . | grep -v "^[ ]*[*]"  | wc
grep -r -h "[^A-Za-z0-9]while (" . | grep -v "^[ ]*[*]"  | wc
grep -r -h "[^A-Za-z0-9]do {" . | grep -v "^[ ]*[*]"  | wc01s you can see, 98% of the loops are not do-while loops.

Moreover, a do-while loop is obviously never required—a student can always write it as a while loop with a boolean flag. For example,

boolean positive = false;
while (!positive)
{  
   System.out.print("Please enter a positive number: ");
   value = in.nextDouble();
   if (value > 0) positive = true;
}

instead of

do
{  
   System.out.print("Please enter a positive number: ");
   value = in.nextDouble(); 
}
while (value <= 0); 

Is the first alternative that much worse?

I thought that downplaying the importance of the do-while loop helps instructors who have bigger fish to fry (such as how to turn a problem statement into a working loop, which is really hard for beginners). However, reviewers were not amused. One of the kinder comments: “This chapter would be vastly improved by moving the do-while into the main body of the text.”

That's indeed what other textbooks do. They give equal billing to each Java loop type. Here is a quote from one briskly selling text:

In general, a for loop may be used if the number of repetitions is known, as for example, when you need to print a message a hundred times. A while loop may be used if the number of repetitions is not known, as in the case of reading the numbers until the input is 0. A do-while loop can be used to replace a while loop if the loop body has to be executed before the continuation condition is tested.

To paraphrase Truman Capote, that's not computer science—it's taxonomy.

(And it's bad taxonomy. Wouldn't “the case of reading the numbers until the input is 0” be one of the few cases where a do-while loop is a better choice?)

But, you may say, an employer wants to make sure that any student whom they hire can read a do-while loop when it does occur, and also write one when it is most appropriate.

I guess this gets to the heart of the difference between certification and education. In my loop chapter, I want to educate students to

  1. Implement loops from a given problem statement
  2. Learn minor technical minutiae on their own as needed

Certification, on the other hand, seems to be mostly concerned with those minor minutiae. Check out these sample questions. I don't see how this it is meaningful for hiring purposes. When I hire coders, erm, software engineers, I want to know whether they can solve problems, not whether they can memorize Java trivia.

Am I getting all worked up over nothing? I could just give in and turn the “advanced topic” into a regular section. Or should I stick to my guns? Please help me make up my mind by posting your thoughts!