7 ms·
Line 457-470 of vicissicalc.c: why do you use else if here rather than switch-case?
by EddSeabrook 12y ago
Line 457-470 of vicissicalc.c: why do you use else if here rather than switch-case?
- ufo 12y agoMight have been because of the `else if (ch == 'q') break;` line. If he used a switch statement he would have needed to use a goto to break out of the loop.
- abecedarius 12y agoThat's a reasonable guess, but a return would work there. I think I did it this way because all the breaks you need in a switch are noisy -- too noisy if you'd like to write one action per line. However, you can mute the noise by lining it up: switch (getchar ()) { break; case ' ': enter_text (); break; case 'f': view = formulas; break; case 'h': col = (col == 0 ? 0 : col-1); which also makes oops-I-forgot-the-break hard to miss. I hadn't thought of that pattern yet. (You could define a macro for "break; case" too; my friend Kragen calls that IF.) But I mostly stopped coding in C after around this time.
- e12e 12y agoThe first break is ignored?
- dlp211 12y agoNot exactly. But it does create a no-op default. I've never seen/used this pattern, so I would have to go compile this down into assembly and play with it to give you a more complete answer.
- abecedarius 12y agoDropped by dead-code elimination. A compiler might conceivably issue a warning that the first break is unreachable, though that's never happened so far.
- kragen 12y agoI thought you were the one who suggested the IF and ELSE macros in http://canonical.org/~kragen/sw/aspmisc/actor.c http://canonical.org/~kragen/sw/aspmisc/actor.c. :) Interestingly, in http://canonical.org/~kragen/sw/dev3/paperalgo http://canonical.org/~kragen/sw/dev3/paperalgo, I haven't yet run into the desire to have more than one `case` in a pattern-matching `switch`. I just added that piece of code from Vicissicalc to the paperalgo page.