The structure for conditional statements (if, if-else, and if-else if-else) are the same as you're used to from C#, so this section should be mostly review. However, there are a couple of minor differences that can sometimes come in handy.
The switch-case statement (again) works, just like in C#, but some languages (C++ for instance) have an additional "fall-through" feature.
Review Questions
The following are examples of questions similar to what you can expect to see on the next exam.
- Do if statements require curly-braces? No, as long as you're only running one statement.
- How do you use the case fall-through feature? Leave off the break statement, and the code will continue to run (fall-through to) the next case.
Consider the following code:
if ("false") cout << "John";
else cout << "Jane";
- What would print to the console, and why? John, because "false" is a string. Therefore, it will resolve to true.
Above and Beyond
The content past here is related, but beyond the scope of the class. In other words, you will not be tested on this!
Ternary Operator
The Ternary Operator is the only operator that takes three operands. It's often used as a shortcut for an if statement.