0% found this document useful (0 votes)
20 views

Lab 4

The document discusses switch statements in C++ including the switch and case keywords. It provides examples of using switch statements to print the number of days in a month, check if a character is a vowel or consonant, create a simple calculator, and calculate grades based on subject marks and percentages.

Uploaded by

hassanimtiaz1h
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Lab 4

The document discusses switch statements in C++ including the switch and case keywords. It provides examples of using switch statements to print the number of days in a month, check if a character is a vowel or consonant, create a simple calculator, and calculate grades based on subject marks and percentages.

Uploaded by

hassanimtiaz1h
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

LAB # 05: Switch structure, while, do while and infinite loops

Tool used: Microsoft Visual Studio

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

The default keyword specifies some code to run if there is no case match.

The break Keyword

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:

Percentage >= 90% : Grade A


Percentage >= 80% : Grade B
Percentage >= 70% : Grade C
Percentage >= 60% : Grade D
Percentage >= 40% : Grade E
Percentage < 40% : Grade F
Using switch statement.

You might also like