SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving
Experiment: 3
PART A
(PART A: TO BE REFFERED BY STUDENTS)
Aim: To study Switch case in C++ programming
Learning Outcomes: Learner would be able to
1. Interpret the scenario to decide on selection blocks.
2. Explain using algorithm and flowchart conditional constructs as per scenario.
Task 1: Identify the errors if any in the following codes without using codeblock and
Justify you answer.
a. #include <iostream>
void main() {
int choice = 2;
switch(choice);
{
case 1:
cout<<"Case 1\n";
break;
case 2:
cout<<"Case 2\n";
break;
case 3:
cout<<"Case 3\n";
break;
case 4:
cout<<"Case 4\n";
break;
default:
cout<<"Case default\n"; }
return 0;
}
b. #include <iostream>
void main()
{
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving
int a=2;
int b=a;
switch(b)
{
case a:
cout<<"Case a \n"; break;
case 3:
cout<<"Case 3\n"; break;
default:
cout<<"No option\n"; break;
}
cout<<"Exit from switch";
}
c. #include <iostream>
int main()
{
char ch='b';
switch (ch)
{
case 'd':
cout<<"Case D ";
break;
case 'b':
cout<<"Case B ";
break;
case 'c':
cout<<"Case C ";
break;
case 'z':
cout<<"CaseZ ";
break;
default:
cout<<"Default";
}
return 0;
}
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving
Task2: Find the output of the following
a. #include <iostream>
int main()
{
int num = 2;
switch (num + 2)
{
case 1:
cout<<"Case 1: ";
case 2:
cout<<"Case 2: ";
case 3:
cout<<"Case 3: ";
default:
cout<<"Default: ";
}
return 0;
}
b. #include <iostream>
void main()
{
int a=10;
switch(a){
case 5+5:
cout<<"Hello\n";
default:
cout<<"OK\n";
}
}
c. #include<iostream>
int main()
{
switch(2/3)
{
case 1:
cout<<"case 1 executed ";
case 2:
cout<<"case 2 execcuted ";
break;
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving
default:
cout<<"Default block executed";
}
return 0;
}
Task 3: Rewrite the following code segment using switch..case.
if ( node == 15)
{ curValue += 5;
Count++;
}
else if (node == 40)
{ curValue *= 1.5;
Count++;
}
else
curValue -= 2;
if (grade == 'A')
cout<<"Very good";
else if (grade== 'B')
cout<<"Good";
else if (grade == 'C')
cout<<"Moderate";
else
cout<<"Try harder!";
Task 4: Write a C++ program to print the day of the week using switch case.
Task 5: Write a C++ program to implement a calculator (a|A for add, s|S for subtract, m|M
for multiply, d|D for division) using switch case.
Task 6: Write a program to give a choice to user to perform below mathematical operations:
a. Finding a square root of number
b. Finding a power of a number
Theory:
Switch case is a branching statement used to perform action based on available choices,
instead of making decisions based on conditions. Using switch case you can write more clean
and optimal code than if else statement. switch case only works with integer, character and
enumeration constants. Switch case statements are a substitute for long if statements that
compare a variable to several integral values.
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving
Working of switch case statement:
The integer expression following switch is evaluated to an integer value.
The integer value is matched one after the other with each of the case constant. When a
match is found the statement block of the matched case is executed till break. On
encountering break the switch statement is exited and controlled transferred after the
switch statement.
Rules to be followed:
It is necessary to include the break statement after every case statement block. If a break
statement is not inserted in the case then the program not only executes the statements
following the case where the match has been met, but also all the following case
statements until break encountered or end of switch.
The default statement is optional, it will be executed if the value does not match any of
the case values.
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving
PART B
(PART B: TO BE COMPLETED BY STUDENTS)
Students must submit the soft copy as per following segments within two
hours of the practical. The soft copy must be uploaded on the portal at
the end of the practical. The filename should be PPS_batch_rollno_experimentno
Example: PPS_B2_B001_Exp1
Roll No.: Name:
Prog/Yr/Sem: Batch:
Date of Experiment: Date of Submission:
Task 1:
Task 2:
Task 3:
Task 4:
Task 5:
Task 6:
Conclusion (Learning Outcomes): Reflect on the questions answered by you jot down your
learnings about the Topic: Conditional Statement.
Home Work Questions:
1. Write an algorithm, draw a flowchart and write a C program to accept the percentage
and print the grade A (90 to 100), B (80 to 90), C (50 to 80) and Fail below 50.
2. Write a C++ program to check whether input character is vowel or consonant.
3. Write a program to give a choice to user to find area of a shape – circle, rectangle,
triangle, square