Title of Project: Switch Case Statement

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION

SHRI H. H. J. B. POLYTECHNIC, CHANDWAD

TITLE OF PROJECT

Switch case statement

Program: EJ Program code:


Course : CPR Course code :22218
MAHARASHTRA STATE
BOARD OF TECHNICAL EDUCATION
Certificate
This is to certify that

Name - …Ahirrao Bhumika bapu

Gholap sweta sanjay …………………………………………………………………………………………………………

Student of II Semester of Diploma in

of Institute, SHHJB POLYTECHNIC, CHANDWAD (Code:0079) has completed the

Micro Project satisfactorily in Subject: C – programing Code: 22218

for the academic year 20 21 - 2022 as prescribed in the curriculum.

Place: chandwad…………………….

Date: ………………………

Subject Teacher Head of the Department Principal


Group Details:

Sr.No Name of group members Roll No Seat No

1 Ahirrao Bhumika 01
bapu
2 Gholap sweta sanjay 12
3
4
5
6
7

Name of Guide:

INDEX
SR.NO. CONTENT PAGE NO.

1
INTRODUCTION

2 SYNTAX

3 ALGORITH

4 FLOW CHART

5
EXAMPLE

6
PROGRAM

9
INTRODUCTION ( Importance of the Project )

Switch statement in C tests the value of a variable and compares it with


multiple cases. Once the case match is found, a block of statements
associated with that particular case is executed.

Each case in a block of a switch has a different name/number which is


referred to as an identifier. The value provided by the user is compared with
all the cases inside the switch block until the match is found.

Rules for switch statement


• An expression must always execute to a result.
• Case labels must be constants and unique.
• Case labels must end with a colon ( : ).
• A break keyword must be present in each case.
• There can be only one default label.
• We can nest multiple switch statement

-
syntax

Switch(variable)

Case1;

Statement block;

Break; Case2;

Statement block;

Break;

Case n;

Statement block;

Break

}
Flow chart of switch statement
Example
#include <stdio.h>
int main() {
int num = 8;
switch (num) {
case 7:
printf("Value is 7");
break;
case 8:
printf("Value is 8");
break;
case 9:
printf("Value is 9");
break;
default:
printf("Out of range");
break;
}
return 0;
}

Output:

Value is 8
2)

#include <stdio.h> int main() { int


number=5; switch (number) { case
1: case 2: case 3:
printf("One, Two, or Three.\n");
break; case 4: case 5: case 6:
printf("Four, Five, or Six.\n");
break; default:
printf("Greater than Six.\n");}}

Output: Four, Five,

or Six.
❖ Write a program

calculate area of

rectangle of circle or

triangle by taking

user choice

Algorithm
1. Start
2. Initilize variable
3. Take input for choice from user
4. Take inpute for area from user
5. Case no 1: circle 3.14 * 3.14* r Case no2: rectangle ar = a*b
Case no 3: triangle at = 0.5 * a*b
6. Display output
7. stop
program
# include<stdio.h>
# include< conio.h>
Void main()
{
Clrscr();
Int ac, ar, at, r, a, b, choice;
Printf(“Enter for choice \n”);
Printf(“ A for area of circle \n”);
Printf (“ B for area of rectangle \n”);
Printf (“C for area of triangle \n”);
Scanf (“ % c”, choice);
Switch ( choice )
Case A
Printf (“ Enter radius”);
Scanf (“% d”, r);
Ac= 3.14 * 3.14* r ;
Printf (“ Area of circle is % d “, Ac);
Break;
Case B
Printf ( Enter height & width”,);
Scanf (“%d%d “, & a, & b);
Ar = a*b;
Printf (“ The Area of rectangle is % d”, Ar);
Break;
Case C;
Printf (“ Enter the value of b&h”,);
Scanf (“ % d % d”, &a ,& b);
At = 0.5 * a*b;
Printf (“ triangle is % d”, At); getch
();
}
Aplication of switch statement:-

The switch statement allows us to execute one code block among many alternatives. You can do the
same thing with the if...else..if ladder. However, the syntax of the switch statement is much easier to read
and write.

Advantage of switch

The switch statement has a fixed depth. It allows the best-optimized


implementation for faster code execution than the “if-else if” statement. It is easy to
debug and maintain the programs using switch statements. The switch statement has faster
execution power.

You might also like