How to Work Switch Case With Proper Example - CodeTextPro | Free Web Tutorial

Codetextpro Provides free Web tutorial, HTML, CSS, C, C++, Java, JavaScript, PHP, MySql, Python, Program, Programming Notes, Programming Questions and more.

Programming

Monday, June 24, 2019

How to Work Switch Case With Proper Example


Switch Statement:

A switch case statement is a multi-way decision statement. It is a simple field version of an if-else block that evaluates only one variable.



Switch Case Program
Switch Case

Syntax of switch case:

Switch (variable)
{
Case 1:
Statement;
Break;


Case 2:
Statement;
Break;
Case n:
Statement;
Break;

Default :
Statement;
Break;
}



Example of a Switch Case Program:

Q. Write a menu driven program in C, press 1 for addition and press 2 for subtraction between 2 number.

#include <stdio.h>
#include <conio.h>
main()
{
int a, b, c, ch;
printf(“Press  1 for Addition.”) ;
printf(“Press  2 for Subtraction.”) ;
printf(“Enter Your Choice”) ;
scanf(“%d”, &ch);
switch(ch)
{
case1:
printf(“Enter 2 Number:”);
scanf(“%d%d”, &a, &b);
c=a+b;
printf(“Addition Result %d”, c);
break;

case2:
printf(“Enter 2 Number:”);
scanf(“%d%d”, &a, &b);
c=a-b;
printf(“Subtraction Result %d”, c);
break;

default:
printf(“Wrong Choice”);
break;
}
getch();
}











No comments:

Post a Comment