1. Intro
The if-else statement extends the plain if statement by adding an alternate branch. Instead of simply skipping code when the condition is false, if-else lets the program execute a completely different block in that case. This makes it the standard way to model a binary decision — such as "eligible or not eligible", "even or odd", or "pass or fail" — in a single, self-contained construct.
Cricket analogy: Deciding 'if run rate is above required, chase confidently, else play defensively' is a binary decision, just as if-else lets a program pick between two complete branches instead of only skipping one.
2. Syntax
if (condition) {
// executed when condition is true
} else {
// executed when condition is false
}3. Explanation
When the program reaches an if-else statement, it evaluates condition exactly once. If it is true, the if block runs and the else block is skipped. If it is false, the if block is skipped and the else block runs instead. Exactly one of the two blocks always executes — never both, and never neither. This guarantees full coverage of both outcomes of a boolean condition, unlike a bare if, which only covers the true case. The else clause has no condition of its own; it simply catches every case not covered by the if.
Cricket analogy: The third umpire checks the no-ball line exactly once per delivery — either it's a no-ball (if-branch) or a fair delivery (else-branch), never both and never neither, giving full coverage of every ball bowled.
4. Example
#include <iostream>
using namespace std;
int main() {
int num = 7;
if (num % 2 == 0) {
cout << num << " is even." << endl;
} else {
cout << num << " is odd." << endl;
}
return 0;
}5. Output
7 is odd.6. Key Takeaways
if-elseprovides exactly two mutually exclusive branches: one fortrue, one forfalse.- Exactly one branch executes per run — never both, never neither.
- The condition is evaluated only once, regardless of which branch runs.
- Useful for binary decisions like even/odd, pass/fail, or eligible/not eligible.
Practice what you learned
1. In an `if-else` statement, how many blocks can execute during a single evaluation?
2. What does the `else` block in an `if-else` statement execute in response to?
3. Which of the following problems is best modeled directly with a plain `if-else` (not a ladder)?
4. How many times is the condition evaluated in `if (cond) {...} else {...}`?
5. What is the key difference between a plain `if` and an `if-else`?
Was this page helpful?
You May Also Like
if Statement in C++
Learn how the C++ `if` statement evaluates a boolean condition and conditionally executes a block of code, with syntax, flow, and examples.
else if Ladder in C++
Learn how the `else if` ladder chains multiple conditions in C++ to test a series of mutually exclusive cases, stopping at the first true match.
Nested if-else in C++
Learn how to place an `if-else` statement inside another `if` or `else` block in C++ to evaluate multiple dependent conditions in sequence.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics