C++20 sections · 1024 units
Open in Course

Nested if Statements

Testing within branches

You can put an if statement inside another if statement's block. I'll show you how to test a second condition only when the first is true. This creates a hierarchy: the inner if only runs when the outer if is true.

You use this when the second question only makes sense after the first answer. Here's an example: if (hasAccount) { if (correctPassword) { login(); } } only checks the password when the account exists.

No account means password check never runs.