Solar wrote:
Using break in switch statements is excused. As for while loops, on the other hand...
Depends where it is.
This is a bad use:
Code:
while (true) {
blahBlahBlah();
if (condition)
break;
}
and so is this (something like this actually appeared in Beej's Guide to Network Programming) (IMO):
Code:
while (condition) {
if (this)
continue;
if (that)
continue;
if (the_other)
continue;
break;
}
It's clear what it's doing but it's still a little ugly.
Would a valid use of break be to neaten up the while statement's condition:
Code:
while (true) {
/* ... */
if (condition_A_which_takes_up_a_lot_of_space ||
(condition_B_which_takes_up_a_lot_of_space_too &&
condition_C_which_takes_up_even_more_space_than_A_and_B))
break;
}
?
IMO, yes. It's still ugly, but to put that in the while loop's clause would be uglier. And I have had to do that once... it was a be-all, end-all line input function. I don't have it any more because the hard disk on the computer I wrote it on is dead. Basically it was to automate doing this:
Code:
printf("What option do you want to pick? [A/B]: ");
fgets(buf, bufsz, stdin);
and it evolved into
Code:
get_input(buf, &options);
options would be an instance of struct gi_opts which said
* What stream to get the input from
* What options to display (A, B, C, Y, N or anything you wanted)
* What to use to divide the options (e.g. "/" or "|") which I removed (unnecessary)
* What to use to surround the options (e.g. "[" would be matched with "]", etc.) which I also removed for the same reason as above
* Which option was the default (-1 for none)
* What to do on invalid input (could be GI_OPTS_EXIT, GI_OPTS_REITER and/or GI_OPTS_PROMPT; the first two being mutually exclusive)
* Max amount of times to reiterate on invalid input (if GI_OPTS_REITER was chosen)
* A prompt to display
Anyway I had to do that in the while loop for the __get_input(char* buf, FILE* stream) function.