⧼⧼  Previous Page
⧼  Table of Contents  ⧽
Next Page  ⧽⧽

Conditional Statements

Computer Programming C++ / Getting Started

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 video doesn't cover the "else if" branch, but as mentioned, it works exactly like it does in C#.

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.

Hover over a question to show the answer.
  1. Do if statements require curly-braces? No, as long as you're only running one statement.
  2. 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";
  1. 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.

⧼⧼  Previous Page
⧼  Table of Contents  ⧽
Next Page  ⧽⧽
© Ryan Appel