Answer:
The if - else and equivalent switch case statements in c++ language are given below.
if ( ch == 1 )
cout << " sum is " << x + y << endl;
else if ( ch == 2 )
cout << " difference is " << x - y << endl;
else if ( ch == 3 )
{
cout << " Quitting " << endl;
exit;
}
switch( ch )
{
case 1 : cout << " Sum is " << x + y << endl;
break;
case 2 : cout << " Difference is " << x - y << endl;
break;
case 3 : cout << " Quitting " << endl;
break;
}
Step-by-step explanation:
The above statements use three variables which are declared and initialized as shown.
int x = 10;
int y = 25;
int ch;
The above statements execute for of addition, subtraction or to quit the program.
cout << " 1. Addition " << endl;
cout << " 2. Subtraction. " << endl;
cout << " 3. Quit " << endl;
cout << " Enter your choice " << endl;
The variable ch holds the numerical option entered by the user.
cin >> ch;
For if - else statements, the if statement is executed based on the value of variable ch entered by the user. After execution, the compiler goes out of the statements automatically.
Following the if – else, if any statement is present, it will be executed.
For switch case, every case ends with break keyword. After the required case is executed based on the value of variable ch entered by the user, the break statement is executed which causes the switch case to terminate the control goes out of the statements.
Following the switch case, if any statement is present, it will be executed.