100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
C++

if-else Statement in C++

Understand how the C++ `if-else` statement provides a two-way branch, executing one block when the condition is true and another when false.

Decision MakingBeginner6 min readJul 7, 2026
Analogies

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

cpp
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

cpp
#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

text
7 is odd.

6. Key Takeaways

  • if-else provides exactly two mutually exclusive branches: one for true, one for false.
  • 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

Was this page helpful?

Topics covered

#CStudyNotes#Programming#IfElseStatementInC#Else#Statement#Syntax#Explanation#StudyNotes#SkillVeris#ExamPrep