do...while vs. repeat...until (was: Errors aren't that simple)

Gary Jackoway gary at hpavla.AVO.HP.COM
Fri Mar 23 23:53:54 AEST 1990


Emuleomo O.O. writes:

> I think this [repeat..until] is preferable  to do  <stmt> while <condition> 

> Why?  Because  using the keyword  while  for two different loop structures
> in a program tends to introduce subtle error that will be hard to find
> into the program.  Whereas, if the keyword   until  was used, there will
> be no room for such errors.
> Consider the following program fragment

I agree with your point, that using the keyword for two purposes can
lead to problems, but your example...

> lots of code
> etc..
> do
> 	etc....
> 	while (some_func(j++)) ; /* This stmt was introduced by a maintenance */
> 							  /* programmer */
> 	a += b;
> 
> while (not_end_of_loop) ;

> Guess what happens to the above code inadvertently??
You can't make this mistake, because a do statement only captures a
single line: do <statement> while (condition).
The way I differentiate "while (condition) do" from  "do <statement> while"
is by ALWAYS putting {} around the do statement(s).  Thus my structure is
do {
statements
} while (condition);
By putting the while on the same line as the curly I also improve readability.
You never stop to wonder whether this is the start of a "while..do".

Gary Jackoway



More information about the Comp.lang.c mailing list