If-Else Statement in C++
Introduction
In real life, we take decisions every day.
-
If it is raining, take an umbrella
-
If marks are good, celebrate
-
Else, study harder
Similarly, in programming, we often need to make decisions.
In C++, this decision-making is done using the if–else statement.
The if–else statement allows a program to choose different paths of execution based on conditions.
What is an If–Else Statement?
An if–else statement checks a condition and executes code based on whether the condition is true or false.
-
If the condition is true, the
ifblock runs -
If the condition is false, the
elseblock runs
This helps the program behave intelligently instead of running the same code every time.
Why Do We Need If–Else in C++?
Without decision-making statements:
-
Programs would run only in one direction
-
No logical thinking would be possible
-
Real-world problems cannot be solved
Uses of if–else:
-
Checking age eligibility
-
Student grading system
-
Login validation
-
Even or odd numbers
-
Banking conditions
-
Game logic
Syntax of If Statement
Example 1: Simple If Statement
Explanation:
-
Condition:
age >= 18 -
Since age is 20, condition is true
-
Output is printed
Syntax of If–Else Statement
Example 2: Even or Odd Number
Explanation:
-
%operator finds remainder -
If remainder is 0 → Even
-
Else → Odd
If–Else If–Else Ladder
When we have more than two conditions, we use else if.
Syntax:
Example 3: Student Grade System
Explanation:
-
Program checks conditions from top to bottom
-
First true condition executes
-
Remaining conditions are skipped
Nested If–Else Statement
A nested if–else means an if inside another if.
Used when decisions depend on multiple levels.
Example 4: Age Category
Explanation:
-
First checks if age ≥ 18
-
Then checks if age ≥ 60
-
Decides category correctly
Relational Operators Used in If Conditions
| Operator | Meaning |
|---|---|
> | Greater than |
< | Less than |
>= | Greater than or equal |
<= | Less than or equal |
== | Equal to |
!= | Not equal |
Logical Operators with If–Else
| Operator | Meaning |
|---|---|
&& | AND |
| ` | |
! | NOT |
Example 5: Login Validation
Real-Life Example: ATM Withdrawal
Common Mistakes in If–Else ❌
❌ Using = instead of ==
❌ Missing { }
❌ Wrong condition order
❌ Extra semicolon after if
Wrong:
Correct:
Difference Between If–Else and Switch
| If–Else | Switch |
|---|---|
| Can check ranges | Cannot check ranges |
| Works with all conditions | Works with fixed values |
| Slower for many cases | Faster for menu-driven programs |
When to Use If–Else?
Use if–else when:
-
Conditions are complex
-
Ranges are involved
-
Logical operators are needed
Advantages of If–Else Statement
✅ Simple to understand
✅ Easy to debug
✅ Powerful decision making
✅ Used in real-world logic
Conclusion
The if–else statement is one of the most important concepts in C++.
It helps programs think logically and make decisions just like humans.
If you understand:
-
if
-
if–else
-
else if ladder
-
nested if–else
Then you have mastered basic decision making in C++.

Comments
Post a Comment