Lesson 16: Case Study- Calculator with Switch statement

Homework: None



Switch Statements
This is the first lesson where we touch on switch statements. If you take a look at them, they work almost exactly the same as an if statement. The only major difference between them is the absolute necessity to use break. Otherwise you can use the same bracket structure (though it’s not required in the cases inside of a switch, I prefer it for readability), and the same flow of logic.

Here’s an example of an if and a switch doing the same thing.

In general I find it easier to avoid switch statements in C++, but some people do feel as though they are used well for menus and things of that nature. It’s also more readable to most people that case is the result of a choice of a value, rather than an arbitrary test (which is what an if portrays). This is the other advantage of switch statements. They are almost always tied to user choices.

Lesson 15: Arrays in Action
Lesson 17: Data validation / Type checking with StringStream