Computer
Programmin
g
• Course Code: CSC-113
• Course Instructor: Mehreen Tariq
• Email:
[Link]@[Link]
Decision Controls
The decision control statements are the decision-making
statements that decides the order of execution of statements
based on the conditions. In the decision-making statements
the programmer specify which conditions are to be executed
or tested with the statements to be executed if the condition
is true or false.
If
1. If statement Conditi
2. If/else statement on
3. If/elseif/else statement
4. Switch statement
5. Nested if/else statement
What is a Switch Statement?
• A multi-way branch statement.
• Allows variable values to be tested against
multiple constants.
• Simpler and cleaner than multiple if-else
statements.
Syntax of Switch Statement
• switch(expression) {
• case constant1:
• // code block
• break;
• default:
• // code block
• }
Rules of Switch Statement
• Expression must evaluate to an integral or
enumerated type.
• Each case must have a constant expression.
• No duplicate case labels allowed.
• Break statement ends a case block.
How Switch Works
• Evaluates expression once.
• Compares it with case labels sequentially.
• Executes matching case’s block until break or
end of switch.
Flow of Switch Statement
• Expression evaluated.
• Control jumps to matching case.
• If no match, default block is executed (if
provided).
• Break ensures exit after executing one case.
Switch vs If-Else
• Switch: cleaner for multiple constant
comparisons.
• If-Else: more flexible for ranges and complex
conditions.
Example 1: Basic Switch
• int day = 3;
• switch(day) {
• case 1: cout << "Monday"; break;
• case 2: cout << "Tuesday"; break;
• case 3: cout << "Wednesday"; break;
• default: cout << "Invalid day";
• }
Output of Example 1
• Since day = 3,
• The program outputs: Wednesday
Break Statement in Switch
• Break prevents execution from falling through
to the next case.
• Without break, multiple cases may execute.
Example 2: Switch without Break
• int num = 2;
• switch(num) {
• case 1: cout << "One";
• case 2: cout << "Two";
• case 3: cout << "Three";
• default: cout << "Other";
• }
Output of Example 2
• Since num = 2 and no break statements:
• Program outputs: TwoThreeOther
The Default Case
• Default is optional but recommended.
• Executes when no case matches the
expression.
• Acts like the 'else' in if-else.
Example 3: Default Case
• char grade = 'B';
• switch(grade) {
• case 'A': cout << "Excellent"; break;
• case 'B': cout << "Good"; break;
• case 'C': cout << "Average"; break;
• default: cout << "Invalid grade";
• }
Nested Switch Statements
• A switch statement inside another switch.
• Useful for multi-level decision making.
Example 4: Nested Switch
• int x = 1, y = 2;
• switch(x) {
• case 1:
• switch(y) {
• case 2: cout << "x=1, y=2"; break;
• }
• break;
• }
Fall-through Cases
• Cases without break execute sequentially.
• Can be used intentionally for grouped cases.
Example 6: Fall-through
• char c = 'a';
• switch(c) {
• case 'a':
• case 'e':
• case 'i':
• case 'o':
• case 'u': cout << "Vowel"; break;
• default: cout << "Consonant";
• }
Switch with Strings?
• C++ switch works only with integral or enum
types.
• Strings cannot be used directly in switch.
• Use if-else for string comparisons.
Limitations of Switch
• No floating-point expressions.
• No ranges or complex conditions.
• Case values must be known at compile time.
Advantages of Switch
• Readable for multiple constants.
• Efficient: compilers optimize switch with jump
tables.
• Preferred for menu-driven programs.
Common Errors
• Forgetting break leads to fall-through.
• Using non-integral types in expression.
• Duplicate case values cause compile-time
error.
Practical Example: Menu Program
• int choice;
• cout << "1. Add\n2. Subtract\n3. Exit";
• cin >> choice;
• switch(choice) {
• case 1: cout << "Addition"; break;
• case 2: cout << "Subtraction"; break;
• case 3: cout << "Exit"; break;
• default: cout << "Invalid";
• }
Write a program in C++ to input a number between 1 to 7 and print the
corresponding day of a week (day name) using Switch statment.
• #include<iostream> • case 3:
• using namespace std; • cout<<"Wednesday";
• int main() • break;
• { int day; • case 4:
• cout<<"\nEnter the Day's • cout<<"Thursday";
number :";
• break;
• cin>>day;
• case 5:
• switch (day)
• • cout<<"Friday";
{ case 1:
• cout<<"Monday"; • break;
• break; • case 6:
• case 2: • cout<<"Saturday";
• cout<<"Tuesday"; • break;
• break; • case 7:
• cout<<"Sunday";
• break; } return 0; }
Best Practices
• Always include break unless fall-through
intended.
• Include default for safety.
• Keep cases simple and short.
Class Activity
Banking Menu Simulation
Write a program using switch that simulates a bank ATM menu:
1 → Check balance
2 → Deposit
3 → Withdraw
4 → Exit
Conditions:
• Balance should update dynamically.
• Withdraw should fail if the amount is greater than balance.
• Use a loop so the menu reappears until user exits.
Class Activity Questions
• 1. Write a switch program that prints month
names given month number (1-12).
• 2. Create a menu program using switch with at
least 5 operations.
• 3. Demonstrate fall-through behavior with
grouped cases.
• 4. Discuss limitations of switch compared to if-
else.
Nested if/else Statement
Sometimes, we need to use an if statement inside
another if statement. This is known as nested if statement.
Think of it as multiple layers of if statements. There is a first,
outer if statement, and inside it is another, inner if statement.
Its syntax is:
// outer if statement
if (condition1)
{
// statements
// inner if statement
if (condition2)
{
// statements
}
}
1. #include <iostream> 12. else {
2. using namespace std; 13. if (b > c) {
14. cout << "Largest = " << b <<
3. int main() { endl;
4. int a, b, c; 15. } else {
5. cout << "Enter three 16. cout << "Largest = " << c <<
numbers: "; endl;
6. cin >> a >> b >> c; 17. }
18. }
7. if (a > b) {
8. if (a > c) {
9. cout << "Largest = " 19. return 0;
<< a << endl; 20. }
10. } else {
11. cout << "Largest = "
<< c << endl; } }
Leap Year Not Leap Year
1968 1971
2004 2006
2012 2010
1200 1700
1600 1800
2000 1900
Leap Year
• If a year is divisible by 400, it is a leap year.
• Example: 2000, 2400 ✅ (Leap years)
• If a year is divisible by 100 but not by 400, it is not a leap year.
• Example: 1700, 1800, 1900 ❌ (Not leap years)
• If a year is divisible by 4 but not by 100, it is a leap year.
• Example: 2024, 2028 ✅ (Leap years)
Leap Year Example
#include <iostream> else {
using namespace std; cout << year << " is NOT a Leap
int main() { Year." << endl;
int year; }
cout << "Enter a year: ";
cin >> year; return 0;
if ((year % 400 == 0) || (year % 4 == 0 && }
year % 100 != 0)) {
cout << year << " is a Leap Year." <<
endl;
}
Reference:
Tony Gaddis, “Starting out with C++”, 6th Edition, Pearson ->
Chapter 01
Summary
• Switch is a control structure for multi-way
branching.
• Works with integers, characters, and enums.
• Cleaner than long if-else chains.
• Has limitations but widely used in C++.