C++20 sections · 1024 units
Open in Course

Choosing Between Two Options

When to use if-else

You use if-else when you have exactly two mutually exclusive options. I'll show you when this pattern fits your problem. Examples include pass/fail decisions, yes/no questions, and even/odd checks.

If you always need one of two outcomes, if-else prevents the case where neither runs. Compare this to a bare if statement, which might run nothing. With if-else, you guarantee that one path executes no matter what the condition is.