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 if block runs

  • If the condition is false, the else block 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

if (condition) { // code to execute if condition is true }

Example 1: Simple If Statement

#include <iostream> using namespace std; int main() { int age = 20; if (age >= 18) { cout << "You are eligible to vote"; } return 0; }

Explanation:

  • Condition: age >= 18

  • Since age is 20, condition is true

  • Output is printed


Syntax of If–Else Statement

if (condition) { // true block } else { // false block }

Example 2: Even or Odd Number

#include <iostream> using namespace std; int main() { int number; cout << "Enter a number: "; cin >> number; if (number % 2 == 0) { cout << "Even number"; } else { cout << "Odd number"; } return 0; }

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:

if (condition1) { // code } else if (condition2) { // code } else if (condition3) { // code } else { // default code }

Example 3: Student Grade System

#include <iostream> using namespace std; int main() { int marks; cout << "Enter marks: "; cin >> marks; if (marks >= 90) { cout << "Grade A"; } else if (marks >= 75) { cout << "Grade B"; } else if (marks >= 50) { cout << "Grade C"; } else { cout << "Fail"; } return 0; }

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

#include <iostream> using namespace std; int main() { int age; cout << "Enter age: "; cin >> age; if (age >= 18) { if (age >= 60) { cout << "Senior Citizen"; } else { cout << "Adult"; } } else { cout << "Minor"; } return 0; }

Explanation:

  • First checks if age ≥ 18

  • Then checks if age ≥ 60

  • Decides category correctly


Relational Operators Used in If Conditions

OperatorMeaning
>Greater than
<Less than
>=Greater than or equal
<=Less than or equal
==Equal to
!=Not equal

Logical Operators with If–Else

OperatorMeaning
&&AND
`
!NOT

Example 5: Login Validation

#include <iostream> using namespace std; int main() { int password; cout << "Enter password: "; cin >> password; if (password == 1234) { cout << "Login successful"; } else { cout << "Wrong password"; } return 0; }

Real-Life Example: ATM Withdrawal

#include <iostream> using namespace std; int main() { int balance = 5000; int withdraw; cout << "Enter amount to withdraw: "; cin >> withdraw; if (withdraw <= balance) { balance = balance - withdraw; cout << "Transaction successful"; } else { cout << "Insufficient balance"; } return 0; }

Common Mistakes in If–Else ❌

❌ Using = instead of ==
❌ Missing { }
❌ Wrong condition order
❌ Extra semicolon after if

Wrong:

if (a = 5); // wrong

Correct:

if (a == 5)

Difference Between If–Else and Switch

If–ElseSwitch
Can check rangesCannot check ranges
Works with all conditionsWorks with fixed values
Slower for many casesFaster 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

Popular posts from this blog

C++ Programming for Beginners – Step by Step Guide

Variables and Data Types in C++