C++ - If / Else
Overview
Estimated time: 25–35 minutes
Control flow with if/else. Learn chained conditions and the ternary operator.
Learning Objectives
- Write conditional logic using if/else if/else.
- Use the ternary operator for simple expressions.
Prerequisites
Examples
int x=5;
if (x > 10) { /*...*/ }
else if (x > 0) { /*...*/ }
else { /*...*/ }
int y = (x > 0 ? 1 : -1);
Common Pitfalls
- Using assignment (=) instead of comparison (==).
Exercises
- Write a function that classifies an integer as negative, zero, or positive.