Switch Case statement - Concept with Example


program to implement switch case statement syntax
In any computer programming language, a switch statement is a type of selection control mechanism used to allow the value of a variable or expression to change the control flow of program execution via a multiway branch.
In this post I shall be explaining to you the concept of a switch case block along with an example to implement the switch-case statement. After going through this tutorial you will be able to understand the concept of switch case statement block and be able to implement switch-case in any problem. 

switch case statement syntax

Syntax:
switch(expression)
{

case 1:
<statement1>;
break;

case 2:
<statement2>;
break;
.
.
.
.
case n:
//where n is the last number number in the series
<statement n>;
break;

default:
<default statement>;
}



Explanation:
In the switch-case statement, the "case" corresponding to the expression in the switch statement gets executed. The execution of statements in a case continues till a break statement is executed.

When a break is encountered, the control is sent out of the switch case block.

The "default" executes the default statement only when the cases declared above does not match the switch expression.



Example:

The following illustrates an example to use a switch statement to find the area of circle, triangle, square, rectangle depending upon the users choice of input

menu driven program using switch case example calculate area




Using C language:


#include<stdio.h>
#include<process.h>
#include<conio.h>
#include<math.h>
void main()
{
float pie=3.14;
int ch,a,b,c,r,s;
double x,y;
clrscr(); //clear screen function
printf("\n***** ** AREA MENU ** *****");
printf("\n\n1.The area of a circle");
printf("\n\n2.The area of a triangle");
printf("\n\n3.The area of a square");
printf("\n\n4.The area of a rectangle");
printf("\n\n5.Exit");
printf("\n\nEnter a choice between 1 to 5\t:");
scanf("%d", ch);
switch(ch)
{

//to find area of a circle
case 1:
printf("\n\nEnter the radius of the circle :");
scanf("%d", &r);
printf("\n\nThe area of the circle is : %d ", (pie*r*r));
break;

//to find area of a triangle
case 2:
printf("\n\nEnter first side of the triangle :");
scanf("%d", &a);
printf("\n\nEnter second side of the triangle :");
scanf("%d", &b);
printf("\n\nEnter third side of the triangle :");
scanf("%d", &c);
s=(a+b+c)/2;
x=s*(s-a)*(s-b)*(s-c);
y=sqrt(x);
printf("\n\nSo the area of the triangle is : %d",y);
break;

//to find area of square
case 3:
printf("\n\nEnter side of the square :");
scanf("%d", &a);
printf("\n\nThe area of the square is : %d",pow(a,2));
break;

//to find area of a rectangle
case 4:
printf("\n\nEnter first side of the rectangle :");

scanf("%d", &a);

printf("\n\nEnter second side of the rectangle :");
scanf("%d", &b);

printf("\n\nThe area of the rectangle is : %d",(a*b));
break;

//exit function
case 5:
exit(0);
break;


//the default statements

default:
printf("\n\nSorry!!! wrong entry!!!");
printf("\n\nEnter a value between 1-5");
}
getch();
}

Using C++:

#include<iostream.h>
#include<process.h>
#include<conio.h>
#include<math.h>
void main()
{

clrscr(); //clear screen function
float pie=3.14;
int ch;
cout<<"\n***** ** AREA MENU ** *****";
cout<<"\n\n1.The area of a circle";
cout<<"\n\n2.The area of a triangle";
cout<<"\n\n3.The area of a square";
cout<<"\n\n4.The area of a rectangle";
cout<<"\n\n5.Exit";
cout<<"\n\nEnter a choice between 1 to 5\t:";
cin>>ch;
switch(ch)
{

//to find area of a circle
case 1:
int r;
cout<<"\n\nEnter the radius of the circle :";
cin>>r;
cout<<"\n\nThe area of the circle is :"<<(pie*r*r);
break;

//to find area of a triangle
case 2:
int a,b,c,s;
cout<<"\n\nEnter first side of the triangle :";
cin>>a;
cout<<"\n\nEnter second side of the triangle :";
cin>>b;
cout<<"\n\nEnter third side of the triangle :";
cin>>c;
s=(a+b+c)/2;
double x=s*(s-a)*(s-b)*(s-c);
double y=sqrt(x);
cout<<"\n\nSo the area of the triangle is :"<<y;
break;

//to find area of square
case 3:
int a;
cout<<"\n\nEnter side of the square :";
cin>>a;
cout<<"\n\nThe area of the square is :"<<pow(a,2);
break;
//to find area of a rectangle
case 4:
int a,b;
cout<<"\n\nEnter first side of the rectangle :";
cin>>a;
cout<<"\n\nEnter second side of the rectangle :";
cin>>b;
cout<<"\n\nThe area of the rectangle is :"<<(a*b);
break;

//exit function
case 5:
exit(0);
break;

default:
cout<<"\n\nSorry!!! wrong entry!!!";
cout<<"\n\nPlease enter between 1-5 ";
}

getch();
}

Comments

Popular posts from this blog

Samsung Galaxy Note 3 - Full Specifications

Run Borland Turbo C/C++ in Android Devices