Lab 4
Lab 4
Lab Description:
Switch case:
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. A switch statement allows a
variable to be tested for equality against a list of values. Each value is called a case, and the
variable being switched on is checked for each switch case.
The default keyword specifies some code to run if there is no case match.
When C++ reaches a break keyword, it breaks out of the switch block. This will stop the
execution of more code and case testing inside the block. When a match is found, and the job is
done, it's time for a break. There is no need for more testing. A break can save a lot of execution
time because it "ignores" the execution of all the rest of the code in the switch block.
Lab Tasks
1. Write a C program print total number of days in a month using switch case.
2. Write a C program to check whether an alphabet is vowel or consonant using switch case.
3. Write a C program to create Simple Calculator using switch case.
4. C++ Program to take the value from the user as input any character and check whether it
is the alphabet, digit or special character. Using the switch statement.
5. C++ Program to calculate profit or loss. Using switch statement.
6. Using switch statement C++ Program to take the value from the user as input marks of
five subjects Physics, Chemistry, Biology, Mathematics, and Computer. Calculate
percentage and grade according to the following: