Decision-making is one of the most important concepts in programming. The computer needs to make choices based on conditions, and that’s where if-else statements come in. They help control the flow of the program by executing certain blocks of code only when conditions are true.

Understanding Flow Control

Flow control refers to the order in which instructions are executed in a program. By default, instructions run sequentially from top to bottom. Decision-making statements, like if-else, allow the flow to branch based on conditions.

  • Without flow control: every line executes in order.

  • With flow control: some code executes only if certain conditions are true, skipping others.

The if-else Statement

The if-else statement is an extension of the if statement. It allows the program to execute one block of code if the condition is true and another block if the condition is false.

Syntax

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

}
else {
// code to execute if condition is false

}

Explanation:

  • condition: A Boolean expression that evaluates to true or false.

  • If the condition is true, the first block executes.

  • If the condition is false, the block after else executes.

Example 1: Simple If-Else

 
#include <iostream>
using namespace std;
int main() {
int age;
cout << "Enter your age: ";
cin >> age;
if (age >= 18) {
cout << "You are eligible to vote." << endl;
}
else {
cout << "You are not eligible to vote." << endl;
}
return 0;
}

Output 1 (age = 20):

 
You are eligible to vote.

Output 2 (age = 15):

 
You are not eligible to vote.
  • The program chooses one path based on the age input.

Flowchart of If-Else Statement

 
             [Start]
                |
           [Condition]
             /   \
          True   False
           /        \
   [Execute]     [Execute]
[True block]      [False block]
          |
        [End]
  • The flowchart shows that only one block executes, depending on the condition.

Example 2: Checking Even or Odd

 
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter a number: ";
cin >> number;
if (number % 2 == 0) {
cout << "The number is even." << endl;
}
else {
cout << "The number is odd." << endl;
}
return 0;
}
  • Real-life analogy: Deciding whether a day is weekday or weekend.

Nested If Statements

Sometimes, you need to check multiple conditions within another condition. This is where nested if statements are used.

Syntax

 
if (condition1) {
// code if condition1 is true
if (condition2) {
// code if both condition1 and condition2 are true
}
}
else {
// code if condition1 is false

}
  • You can nest multiple levels of if statements, but avoid too many levels to keep code readable.

Example 1: Nested If

 
#include <iostream>
using namespace std;
int main() {
int marks;
cout << "Enter your marks: ";
cin >> marks;
if (marks >= 50) {
cout << "You passed the exam." << endl;
if (marks >= 90) {
cout << "You got an A+ grade!" << endl;
}
}
else {
cout << "You failed the exam." << endl;
}
return 0;
}

Output 1 (marks = 95):

 
You passed the exam.
You got an A+ grade!

Output 2 (marks = 60):

 
You passed the exam.

Output 3 (marks = 40):

 
You failed the exam.
  • Nested if allows checking more than one condition in a structured way.

Example 2: Nested If with Multiple Grades

 
#include <iostream>
using namespace std;
int main() {
int marks;
cout << "Enter your marks: ";
cin >> marks;
if (marks >= 90) {
cout << "Grade: A+" << endl;
}
else {
if (marks >= 75) {
cout << "Grade: A" << endl;
}
else {
if (marks >= 60) {
cout << "Grade: B" << endl;
}
else {
if (marks >= 50) {
cout << "Grade: C" << endl;
}
else {
cout << "Fail" << endl;
}
}
}
}
return 0;
}
  • Better approach: Use else if ladder (more on this later).

  • Nested if helps in step-by-step decision-making.


Real-Life Example 1: Traffic Signal Decision

 
#include <iostream>
using namespace std;
int main() {
char signal;
cout << "Enter traffic signal color (R/Y/G): ";
cin >> signal;
if (signal == 'R') {
cout << "Stop!" << endl;
}
else {
if (signal == 'Y') {
cout << "Get Ready!" << endl;
}
else {
if (signal == 'G') {
cout << "Go!" << endl;
}
else {
cout << "Invalid color!" << endl;
}
}
}
return 0;
}
  • Nested if can handle multiple conditions step by step.

Real-Life Example 2: Bank ATM PIN Check

 
#include <iostream>
using namespace std;
int main() {
int pin;
cout << "Enter your PIN: ";
cin >> pin;
if (pin == 1234) {
cout << "PIN is correct." << endl;
int amount;
cout << "Enter withdrawal amount: ";
cin >> amount;
if (amount <= 5000) {
cout << "Transaction successful!" << endl;
}
else {
cout << "Insufficient balance!" << endl;
}
}
else{
cout << "Incorrect PIN!" << endl;
}
return 0;
}
  • Nested if helps check multiple security layers.