Ch 6 · Conditional Statements · C++

Topic 6 of 13 in C++ — Complete Tutorial (Beginner) — 2 lessons.

if / else if / else

A conditional statement lets your program take different actions depending on a condition. You write if, a condition in round brackets, then a block in curly braces that runs only when the condition is true. Add else if for more options and else as the catch-all. C++ checks conditions top to bottom and runs the first true one, skipping the rest.

switch and the Ternary

When comparing one variable against many specific values, a switch is cleaner than a long else if chain. Each case needs a break to stop falling through to the next; default is the catch-all. For a quick two-way choice producing a value, the ternary operator condition ? a : b is a compact one-line if-else.

All topics in C++ Beginner

  1. Introduction to Programming & C++
  2. Your First C++ Program
  3. Variables & Data Types
  4. Input & Output
  5. Operators & Expressions
  6. Conditional Statements
  7. Loops & Iterations
  8. Functions
  9. Vectors & Strings
  10. Classes & Objects
  11. Working with Classes
  12. Exception Handling
  13. Mini-Projects & Practice