A sample C++ program to demonstrate the use of a "do... while" loop and a "switch" statement
A program in c++ which accepts two integer values and performs addition, subtraction, multiplication and division using a switch statement, while keeping it in a loop
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,choice; // declaration
clrscr();
cout<<"The value of A is ="<<endl;
cin>>a; // value of A is entered & stored
cout<<"The value of B is ="<<endl;
cin>>b; // value of B is entered & stored
do // loop begins
{
cout<<"1. Addition"<<endl;
cout<<"2. Subtraction"<<endl;
cout<<"3. Multiplication"<<endl;
cout<<"4. Division"<<endl;
cout<<"5. Exit Program"<<endl;
cout<<"Enter Your Choice , Hit 5 to Quit"<<endl;
cin>>choice;
switch(choice) //switch statement begins
{
case 1 : cout<<"Sum of A & B is ="<<a+b<<endl; // Addition
break;
case 2 : cout<<"Difference of A & B is ="<<a-b<<endl; // Subtraction
break;
case 3 : cout<<"Product of A & B is ="<<a*b<<endl; // Multiplication
break;
case 4 : cout<<"Division of A & B is ="<<a/b<<endl; // Division
break;
default : cout<<"Enter a valid choice "<<endl;
break;
}
}while(choice != 5); // if choice is greater than 5, program ends along with loop
}