0% found this document useful (0 votes)
16 views4 pages

Lab Manual-4

The document discusses the use of switch statements in C/C++ for selection control, highlighting their advantages over if/else statements. It includes examples of switch statements with break statements to prevent fall-through behavior and outlines two programming tasks: creating a calculator program and a grade converter using switch statements. Both tasks require user input and appropriate handling of invalid cases.

Uploaded by

adanzahra19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views4 pages

Lab Manual-4

The document discusses the use of switch statements in C/C++ for selection control, highlighting their advantages over if/else statements. It includes examples of switch statements with break statements to prevent fall-through behavior and outlines two programming tasks: creating a calculator program and a grade converter using switch statements. Both tasks require user input and appropriate handling of invalid cases.

Uploaded by

adanzahra19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

PROGRAMMING FUNDAMENTALS

 Learning how to use Switch statement, Case statement, Break


statement.

Learning how to use Switch statement, Case statement, Break statement


and Continue Statement.

Switch statement is C/C++ language is used for selection control. The difference
between if/else and switch selection statements is that the second one is used for
making a selection from multiple statements.
There are times when you'll find yourself writing a huge if block that consists of
many else if statements. The switch statement can help simplify things a little.
It allows you to test the value returned by a single expression and then execute
the relevant bit of code.
You can have as many cases as you want, including a default case which is
evaluated if all the cases fail. Let's look at the general form.
switch (expression)
{
case expression1:
/* one or more statements */
case expression2:
/* one or more statements */
/* ...more cases if necessary */
default:
/* do this if all other cases fail */
}
PROGRAMMING FUNDAMENTALS

#include <iostream>
using namespace std;
int main()
{
int a;
cin>>a;
switch (a) { case 1:
cout<<"You chose number 1\n";
case 2:
cout<<"You chose number 2\n"; case 3:
cout<<"You chose number 3\n"; case 4:
cout<<"You chose number 4\n"; default:
cout<<"That's not 1,2,3 or 4!\n"; You'll notice that the program will select the
} correct case but will also run through all the
return 0; cases below it (including the default) until the
} switch block's closing bracket is reached.
To prevent this from happening, we'll need to
insert another statement into our cases...

Break:
The break statement terminates the execution of the nearest enclosing switch statement in which
it appears. Control passes to the statement that follows the terminated statement
#include <iostream>
using namespace std;
int main()
{
int a;
cin>>a;
switch (a) { case 1:
cout<<"You chose number 1\n"; break;
case 2:
cout<<"You chose number 2\n"; break;
case 3:
cout<<"You chose number 3\n"; break;
case 4:
cout<<"You chose number 4\n"; break;
default:
cout<<"That's not 1,2,3 or 4!\n";}
return 0;
}
PROGRAMMING FUNDAMENTALS

Task 01: Calculator Program using Switch Statement


You are required to create a simple calculator program that takes an operator (+, -, *, /) and two
numbers as input from the user and performs the corresponding arithmetic operation using a
switch statement.
Your program should:
1. Display a prompt for the user to enter an operator (+, -, *, /).
2. Allow the user to enter two numbers.
3. Use a switch statement to determine the operator and perform the appropriate calculation.
4. Display the result of the operation.
Ensure that your program handles the following cases:
 If the user enters an invalid operator, the program should display an error message.
 If the user attempts to divide by zero, the program should display an appropriate error
message.
Task 02: Grade Converter
You are tasked with creating a program that takes a numerical grade as input and converts it into
a letter grade using the following scale:
 A: 90-100
 B: 80-89
 C: 70-79
 D: 60-69
 F: 0-59
Your program should:
1. Display a prompt for the user to enter a numerical grade.
2. Use a switch statement to determine the corresponding letter grade based on the input.
3. Display the calculated letter grade.
PROGRAMMING FUNDAMENTALS

You might also like