Switch Statement
The switch statement is an alternative approach for the if statement. It uses case structure instead of if – else – if ladder.
Syntax:
Switch(data to be evaluated)
{
case specific value:
//execute instruction here
break;
case specific value:
//execute instruction here
break;
.
.
.
default:
break;
}
Notice that case will evaluate specific data, does not use relational operators and has a break after each instruction inside each case. The default is an optional case just like an else in if statement.
Sample program:
Given the table below for the rate of an employee, write a program that computes for the monthly salary.
Position Rate per Hour
<M>anager $40.00
<S>upervisor $30.00
<O>ffice Staff $15.00
The program will prompt the user to enter the position and no. of hours worked. The program will then compute for the monthly salary without any deduction from the tax, social security, etc. Assume that the number of days in a month is 22 days (4 weeks in a month and 5 days a week plus 2 extra days).
Computation:
Monthly Salary = rate per hour * no. of hours worked * no. of days
Sample Program code using switch statement
#include<iostream>
using namespace std;
int main( )
{
string position;
char p;
int days = 22;
float msalary, rate,hours;
cout <<"Company Position"<<endl<<endl
<<"\t<M>anager"<<endl
<<"\t<S>upervisor"<<endl
<<"\t<O>ffice Staff"<<endl
<<"\tPosition : ";
cin>>p;
switch(p)
{
case 'm': rate = 40.00;
position = "Manager";
break;
case 's': rate = 30.00;
position = "Supervisor";
break;
case 'o': rate = 15.00;
position = "Office Staff";
break;
}
cout<<endl<<"\tNo. of hours worked per day: ";
cin>>hours;
msalary = rate * hours * days;
cout <<endl<<"\tThe monthly salary for the"<<endl
<<"\tPosition of "<<position<<endl
<<"\twith a rate of $"<<rate<<" is $"<<msalary<<endl;
return 0;
}
Sample Output:
Company Position:
<M>anager
<S>upervisor
<O>ffice Staff
Position : s
No. of hours worked per day: 6
The monthly salary for the
Position of Supervisor
with a rate of $30 is $3960
Company Position:
<M>anager
<S>upervisor
<O>ffice Staff
Position : s
No. of hours worked per day: 6
The monthly salary for the
Position of Supervisor
with a rate of $30 is $3960
In this sample program, the user is given a possible position to choose. That is using a cout command. The user is allowed to interact with the program using cin where the data entered by the user is stored in the variable p. The variable p is then evaluated using the switch statement. Again, here in switch statement, it is using a case structure rather than the if else combination. Case ‘m’, if the value of p is m, then the variable rate is assigned with a value of 40.00 and variable position is assigned with the value of “Manager” where it is its equivalent. And so with the other cases. Notice that there is a break after each case, this is to terminate the switch statement.
The next procedure is where the program prompts for the no. of hours worked per day and the process of computation where you can find the formula of the monthly salary which is represented by the msalary variable. Finally, The salary of the position with its rate is displayed.
No comments:
Post a Comment