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

Lab 4 - Nested If and Switch

The document discusses nested if and switch/case structures in C++. It explains that a switch-case statement can select one of many code blocks to execute based on a variable's value matching multiple cases. If no case matches, the default statement executes. Rules for switch-case include expressions evaluating to a result and unique case labels ending with colons. Break keywords terminate each case's execution. Nested switch-case statements can select code blocks within other switch-case blocks.
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)
68 views

Lab 4 - Nested If and Switch

The document discusses nested if and switch/case structures in C++. It explains that a switch-case statement can select one of many code blocks to execute based on a variable's value matching multiple cases. If no case matches, the default statement executes. Rules for switch-case include expressions evaluating to a result and unique case labels ending with colons. Break keywords terminate each case's execution. Nested switch-case statements can select code blocks within other switch-case blocks.
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

Nested If and Switch/Case Structure

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
}

This is how it works:

• The switch expression is evaluated once


• The value of the expression is compared with the values of each case
• If there is a match, the associated block of code is executed
• The break and default keywords are optional, and will be described later in the tutorial

Why Need Switch-Case?


The complexity of a program increases whenever the number of the alternative path increases. If
multiple if-else statements are used, the program might get difficult to read.

Rules for Switch-Case?


• An expression must always execute to a result
• Case labels must be constant and unique
• Case labels must end with a colon
• break keyword must be present for each case
• There can be only one default label
• We can nest multiple switch-case statements
Example:
The example below uses the weekday number to calculate the weekday name:
Code:

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.
The default keyword:
The default keyword specifies some code to run if there is no case match:
Example

Multiple Cases in Switch-Case


The switch case statement consists of a series of labels, an optional default case, and the statement
to execute for each case.
Example
Using a switch-case statement, write a program to take a month from the user and display the
number of days each month has. Assume that February has only 28 days (ignore leap year).
Nested Switch-Case
A switch-case statement inside another switch-case statement is termed a nested switch-case.
Syntax:
switch(expression 1) {
case x:
// code block
break;
case y:
// code block
break;
case z:
// code block
switch(expression 2) {
case a:
// code block
break;
case b:
// 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

You might also like