Control Statements: If, Else & Switch in C++
Imagine you're in charge of a game room.
If a player scores above 90, they win a gold badge. If they score above 70, they get silver. If they score less, they get bronze.
You are making decisions based on conditions, and computers do the same using if, else, and switch statements.
In this lesson, you’ll teach C++ how to make these decisions on its own.
Think of it as giving your program the ability to think, react, and choose between different paths.
Your First Digital Grading System
You’ll build a small app that determines a student’s grade based on their marks.
Here’s how it should look:
1 2 3 4 5======================== STUDENT GRADE Marks: 85 Grade: A ========================
Your program will check the marks and decide which grade to print, just like a real school system.
The if Statement
An if statement checks whether a condition is true. If it is, C++ runs the code inside it. This is how programs react to specific situations.
Example
1 2 3if (marks >= 90) { cout << "A+"; }
Here, the message “A+” appears only if the marks are 90 or above.
The else Statement
The else block runs when the if condition is not met. It provides an alternate action when the first condition fails.
Example
1 2 3 4 5if (marks >= 90) { cout << "A+"; } else { cout << "Below A+"; }
If marks are not 90+, the second line is printed instead.
The else if Chain
Use else if when you need multiple possible outcomes. It lets your program pick the correct grade from many ranges.
Example
1 2 3else if (marks >= 80) { cout << "A"; }
If the first check fails, this one is tested next.
Nested Conditions
A nested condition is an if inside another if. You use it when one decision depends on another.
Example
1 2 3if (marks >= 50) { if (marks >= 90) cout << "Distinction"; }
First it checks if the student passed, then checks for distinction.
The switch Statement
A switch statement is perfect for choosing between many fixed options, like menu choices.
It compares one value against several cases.
Example
1 2 3 4 5switch(option) { case 1: cout << "Add"; break; case 2: cout << "Subtract"; break; default: cout << "Invalid"; }
Only the matching case runs, making menus clean and simple.
Let’s Build It
Here is your complete grading program in C++:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28#include <iostream> using namespace std; int main() { int marks = 85; char grade; if (marks >= 90) { grade = 'A'; } else if (marks >= 80) { grade = 'B'; } else if (marks >= 70) { grade = 'C'; } else { grade = 'F'; } cout << "========================" << endl; cout << " STUDENT GRADE" << endl; cout << " Marks: " << marks << endl; cout << " Grade: " << grade << endl; cout << "========================" << endl; return 0; }
The if–else ladder checks the marks and assigns the correct grade. When you change the value of marks, the grade updates automatically.
How It Works
- The program stores the marks in a variable.
- The if–else chain evaluates each condition one by one.
- The matching condition assigns the correct grade.
- The final print section creates a clean, formatted result.
This is your program’s first real “decision-making” ability, a huge step forward.
Learn Together with AI
You can ask your AI Copilot to help you write or modify decision-based programs.
Try saying:
“Write a C++ grading program using if, else if, and else blocks.”
Then try experimenting:
- Add a new grade for marks above 95.
- Print a motivational message for high achievers.
- Ask AI to turn your if–else chain into a switch statement.
Every change teaches you how decision logic works in C++.
Practice Time
Write a C++ program that prints a food order based on a menu choice.
Here’s the required output format:
1 2 3 4 5======================== FOOD ORDER Item: Burger Price Category: Affordable ========================
Your program must:
- Use switch to print one of these items: Pizza, Burger, Sandwich, Biryani.
- Use if–else to print:
- “Expensive Item” if price ≥ 200
- “Affordable Item” otherwise
Use variables for both item and price.
Frequently Asked Questions
You’ll learn how C++ makes decisions using if, else, else-if, nested conditions, and switch statements. These tools let your programs react to different situations.
An if statement checks a condition and runs code only when that condition is true. It’s how programs choose when to perform certain actions.
Use else when you want a fallback action. Use else if when you want to check multiple conditions in sequence.
Switch is ideal for menu choices or scenarios with several fixed options (e.g., 1 = Add, 2 = Subtract). It keeps the code clean and readable.
Absolutely. You can ask AI to rewrite your conditions, explain logic errors, or generate new examples to practice.
Still have questions?Contact our support team