Lab # 05: Nested Selection/Decision control structures (if else and switch
case) in C++
Instructor: Yahya Jan
Note: Submit this lab hand-out in the next lab with attached solved activities and exercises.
Submission Profile
Name: CMSID:
Submission date (dd/mm/yy): Marks obtained:
Comments:
______________________________________________________________________________
_
Instructor Signature
Lab Learning Objectives:
After going through this lab student would be able to:
1. Understand Nested Conditions in C++
2. Compare Compound and Nested Conditions in C++
3. Using nested if/else and switch cases in C++.
Lab Hardware and Software Required:
1. Computer System, Dev C++ IDE.
Background Theory:
Nested Conditions in C++
Nested conditions refer to placing one conditional statement inside another. This allows programs to make
more complex decisions by evaluating multiple layers of conditions.
Why Use Nested Conditions?
• To handle multi-step decision-making.
• To group related conditions logically.
• To respond differently based on multiple criteria.
Syntax of Nested if Statements:
if (condition1) {
// executes if condition1 is true
if (condition2) {
// executes if condition2 is also true
}
}
Example 1: Check if a number is positive and even
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
if (num > 0) {
if (num % 2 == 0) {
cout << "The number is positive and even." << endl;
} else {
cout << "The number is positive but odd." << endl;
}
} else {
cout << "The number is not positive." << endl;
}
return 0;
}
Compound VS Nested Conditions in C++
Compound Conditions
Compound conditions combine multiple logical expressions using logical operators:
• && (AND)
• || (OR)
• ! (NOT)
Example 2: Compound Condition
if (age > 18 && age < 60) {
cout << "You are eligible to work." << endl;
}
Feature Nested Conditions Compound Conditions
Structure One condition inside another Conditions combined in one statement
Readability May be harder if deeply nested Easier for simple logic
Use case When further checking is needed When multiple checks give a single result
Flexibility More flexible, allows different paths More concise, but less flexible
Example 3: Comparing both
Nested:
if (score >= 50) {
if (score <= 100) {
cout << "Valid passing score." << endl;
}
}
Compound:
if (score >= 50 && score <= 100) {
cout << "Valid passing score." << endl;
}
Using Nested if/else and switch Cases in C++
Nested if/else Statements
Used when you have multiple outcomes based on different conditions.
Example 4: Grading System Using Nested if/else
#include <iostream>
using namespace std;
int main() {
int score;
cout << "Enter your score: ";
cin >> score;
if (score >= 90) {
cout << "Grade: A" << endl;
} else {
if (score >= 80) {
cout << "Grade: B" << endl;
} else {
if (score >= 70) {
cout << "Grade: C" << endl;
} else {
cout << "Grade: F" << endl;
}
}
}
return 0;
}
Nested switch Cases
Although nesting switch is uncommon, it can be used when multiple levels of choices are required.
Syntax:
switch (expression1) {
case value1:
switch (expression2) {
case value2:
// code
break;
}
break;
}
Example 5: Nested switch for Menu Selection
#include <iostream>
using namespace std;
int main() {
int mainChoice, subChoice;
cout << "Main Menu:\n1. Electronics\n2. Clothing\nEnter your choice: ";
cin >> mainChoice;
switch (mainChoice) {
case 1:
cout << "Electronics Submenu:\n1. Mobile\n2. Laptop\nEnter your choice: ";
cin >> subChoice;
switch (subChoice) {
case 1:
cout << "You selected Mobile." << endl;
break;
case 2:
cout << "You selected Laptop." << endl;
break;
default:
cout << "Invalid sub-choice." << endl;
}
break;
case 2:
cout << "You selected Clothing." << endl;
break;
default:
cout << "Invalid main choice." << endl;
}
return 0;
}
Summary
Concept Description
Nested if if inside another if, used for step-by-step decisions
Compound conditions Use logical operators (&&, `
Nested if/else Allows multi-path decisions depending on layered conditions
A switch inside another switch, useful for complex menus or category
Nested switch
selections
Best Practice Avoid excessive nesting for readability; prefer else-if ladder or compound logic
Lab Activities:
1. Write a C++ program that calculates the income tax for a given annual income. The tax is
calculated based on the following slabs:
a. Up to 2,50,000: No tax
b. 2,50,001 to 5,00,000: 5% tax
c. 5,00,001 to 10,00,000: 20% tax
d. Above 10,00,000: 30% tax
The program should ask the user to enter their annual income and display the tax payable
based on the income slab they fall into. The tax should be calculated as per the given slabs.
Additionally, if the entered income is less than 0, the program should display an error
message stating that the income cannot be negative.
Requirements:
• Use nested if-else statements to determine the tax slab.
• Ensure that the program handles negative income properly with a custom error
message.
• The program must handle non-negative income values only.
Input: _________________________
Output: ________________________
2. Write a C++ program that functions as a simple calculator. The program should take three
inputs from the user:
a. A number (first operand)
b. An arithmetic operator (+, -, *, /)
c. Another number (second operand)
The program should perform the corresponding operation based on the operator entered
and display the result. The program must handle the following conditions:
• If the operator is +, the program should add the two numbers.
• If the operator is -, the program should subtract the second number from the first
number.
• If the operator is *, the program should multiply the two numbers.
• If the operator is /, the program should check if the second number is zero. If the
second number is zero, it should display an error message "Error: Division by
zero is not allowed." Otherwise, it should perform the division.
• If the user enters an invalid operator (something other than +, -, *, or /), the
program should display an error message saying "Error: Invalid operator
entered."
Requirements:
• Use a switch-case statement to handle different operators.
• Use nested if-else statements to check for division by zero and invalid operators.
• Display appropriate messages for each operation or error case.
Input: ________________________
Output:_______________________
3. Write a C++ program that takes a single character input from the user and performs the
following checks:
• Determine whether the character is a letter (A–Z or a–z).
• If it is a letter:
o Check whether it is a vowel (A, E, I, O, U, a, e, i, o, u) or a consonant.
o Identify whether the letter is in uppercase or lowercase.
• If the character is not a letter, output a message stating "The character is not an
alphabet."
Requirements:
• Use nested if-else statements to:
o Verify if the character is a letter.
o Determine case (uppercase/lowercase).
o Check if it's a vowel or consonant.
o Use a switch statement to match vowels.
Constraints:
• The program must only accept a single character input.
• You must use only nested conditions and switch-case — no loops or arrays.
• Handle both lowercase and uppercase letters properly.
Input: ________________________
Output:_______________________