Lab 4 - Nested If and Switch
Lab 4 - Nested If and Switch
Switch-Case Statement:
Use the switch statement to select one of many code blocks to be executed. Test values of a
variable and compares it with multiple cases. Once the case is matched, a lock of statement
associated with that particular case is executed.
If a case is not matched, the default statement is executed and the control goes out of the switch
block
Syntax:
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
TASKS:
1. Write a program that inputs three different integers from user, then prints the sum, the average,
the product, the smallest and the largest of these numbers. Use selection statement.
2. Write a C++ program to check whether a character is an uppercase or lowercase alphabet.
Using the selection statement.
3. Write a program that take an integer from the user and check whether an integer is positive,
negative, or zero using switch case statement.
4. Write a C++ program to take the value from the user as input any alphabet and check whether
it is vowel or consonant. Using the switch statement.
5. Write a program which performs arithmetic operations on integers using switch. The user
should enter the two integer values and the arithmetic operator.
a) Addition (+)
b) Subtraction (-)
c) Division (/)
d) Multiplication (*)
e) Modulus (%)
6. Write a program which to find the grace marks for a student using switch. The user should
enter the grade obtained by the student and the number of subjects he has failed in.
• If the student gets A grade and the number of subjects, he failed in is greater than 3,
then he does not get any grace. If the number of subjects he failed in is less than or
equal to 3 then the grace is of 5 marks per subject.
• If the student gets B grade and the number of subjects, he failed in is greater than 2,
then he does not get any grace. If the number of subjects he failed in is less than or
equal to 2 then the grace is of 4 marks per subject.
• If the student gets C grade and the number of subjects, he failed in is greater than 1,
then he does not get any grace. If the number of subjects he failed in is equal to 1 then
the grace is of 5 marks per subject