One thing though, the link you provided me to show a menu, wow it is way more complex then a simple do while loop! I don't understand all the hate with a do while loop, the conditions are simple, the execution is easy and the logic is easy to understand! |
All 3 of the loop types can be converted from one to another, so maybe at the price of an extra variable we can avoid
do
loops.
do
loops can be error prone. The main reason people might choose to use one, is that value of a variable in the condition is not known until some time in the body of the loop. The fact that it always executes at least once is not a reason to prefer a do loop IMO.
It is the ugly condition that I hate, I am not a
fan of do loops. Do some situations where a do loop is handy.
Kernighan and Ritchie C Programming wrote: |
---|
Experience shows that do-while is much less used than while and for. Nonetheless, from
time to time it is valuable, as in the following function itoa, which converts a number to a
character string (the inverse of atoi). |
the example you provided looks like its all over the place |
Probably because I adapted the OP's code from that topic, back then. It is a little more complex, but it does what we want: Loops until quit, shows a menu, and has a
default:
case for bad input. I usually prefer to have each
case
call it's own function, that is more tidy :+)
There is a direction relation between
switch
and an
if
else if
else
chain, One has to use the latter if the variable is not a compile time constant
int
or
char
.