Skip to main content

Much A-do About Nothing

Posted by cayhorstmann on April 6, 2009 at 6:04 PM PDT

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 "^[ ]*[*]"  | wc

As 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 href="http://www.sun.com/training/certification/assessment/055.jsp">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!

Comments

Ditto what Bruce Chapman said.

Cay, leaving it out somewhat seems like a good idea, but calling it "advanced" is maybe where the problem lies. Maybe a section title that emphasises the low usage in practice rather than hinting at complexity (which doesn't really exist relative to "while" ) might keep the reviewers happy, and allow you to keep to your ideals. Bruce

cajo: do-while is NOT in the same category as finally. Students absolutely need to know how to use finally for resource cleanup. (Oddly enough, some of those briskly selling textbooks treat finally as an exotic topic--go figure...) dwalend: Indeed I take the same approach with the ? : operator. It's nice to know but not considered very important. (In my experience. students tend to pick it up on their own anyway because they enjoy the saved keystrokes :-))

Cay, keep it up! You're one of a few who are not afraid to remove the unpopular and rarely used stuff and replace it with something more interesting. I think that do-while does deserve a quick mentioning and that is about it. Big Java is an excellent book. I had the pleasure of reading the 2nd edition. It is about learning to program i.e. solving problems with code and not about learning constructs and doing construct related workouts - that is a side effect of solving a problem not the main objective. You're book and writing style gets it right! I am not against learning "constructs" it is just that usually one learns more about constructs and how they are implemented from a course in "Programming Languages" and not from Introduction to Programming.

Cay, The most important bit of trivia that's important about do-while is to contrast it with repeat-until. Pointing out the difference would save useless aggravation for the student. What's your opinion of the ? operator? It seems like the same sort of choice. Dave

Hi Cay, I lump do-while in the same category as finally; you can of course get by without them -- but, there are times when they can so eloquently express an idea... it exists simply to add expressive richness to the language. Perhaps consider providing some examples, where using the other types of loops would look clumsy. I feel it is not an "advanced topic" at all, but rather an exploration into alternate constructs, and different ways of thinking. John

Cay, I think it's worth pointing out the different philosophy between "while" and "do-while" as an example of why folks need to think before they code. The former makes no assumption that the body of the loop will ever be executed, the latter asserts that the body of the loop will be performed at least once. I agree with you that the language would have been just fine leaving out "do-while"... but since it is there you might as well use it as an entry into programming style and philosophy. -JohnR

>Am I getting all worked up over nothing? I think you kind of are. I agree that in practice, do-while is used a lot less. It seems weird to call it an "advanced topic" though since I don't consider it any more advanced than the other loops. It's just another detail of the exact same topic. I like the idea of having the students solve the same looping problem with each of the different loop types. I think it helps them understand the differences between the loop types. I realize it may seem like minutiae, but I think that understanding the differences between the different loop types helps give beginners a better sense of what loops are really doing for them. I have to ask, does the do-while really slow down the curriculum that much?

Certification do not endorse education, that's obvious.. and it never will.. it is just a memory test, like sudoku and others :) But certification proves discipline and resilience .. ability to memorize and to keep focus under pressure.. it is like an amateur athlete finishing a marathon.. he is not the best, eventually he is not even born to do that.. but he proved he has strengths and determination to reach his goals.. and this is also important :) * in Brazil (and I suppose in several other countries) it is very common to find young students collecting SUN certifications, some of them with more than 5 and just twenty fews year old.. :) * In Switzerland, people barely remember such certifications exist.. and they only apply for such tests due to the obviousness of the market.. rule of thumb: more educated the people, less interested they are in memory tests..

I cannot comment much about pedagogy - my classes are only towards older people and if I want to make some "brainful" divagation it's more about design. But I agree with you on the scarce relevance of do-while. I also believe that if I go and look at my own code, I seldom use the while itself. For instance the above example could be rewritten as (of course, now the code formatting will be ruined): for (boolean positive = false; !positive; ) { System.out.print("Please enter a positive number: "); value = in.nextDouble(); if (value > 0) positive = true; } What I appreciate about the for is the capability of using a local scope for the needed variable (which is what you need 95% of times - i.e., you don't need to read the iteration variable outside of the loop). Of course, this point about the local scope has very limited consequences of the code - it's not even taxonomy, rather aesthetics.

Now if the students can understand and explain the grep statements at the top that will be real education. All joking aside, it may not be that bad for students if you ask them to do the same problem with and without the do-while loops in terms of exercising their brains. You contradict yourself somewhat, because your statistics are clearly significant from a utilitarian (read vocational) perspective but you want to make a decision on the basis of what is more educational.