r/learncpp • u/victotronics • Feb 05 '19
What are case statements for?
Yes, I know how they work and all. Here's my situation: I teach introductory programming, and I'm showing my students conditionals and case statements. And I really can not think of an example where a case statement is essentially more elegant or appropriate than a conditional. Is there? I guess having the fall through to the next case allows for some compactness, but I'd like to see a scenario for that that I can distill to a convincing demonstration.
Now case statements in shells, those I can defend because they can be used for wild-card matching of strings. Numerical cases, even in Fortran where you can have ranges, I just don't see.
View all comments
Show parent comment
1
u/victotronics Mar 29 '19
Indeed. Do you have a practical example of this?