Suppose I want to calculate net_salary of an employee after taking into account the number of years he's worked and the number of kids he has. I don't want to use nested if statements since that will complicate the number of checks I need to make.
double base_salary, net_salary;
int nmbr_kids, nmbr_years;
if(nmbr_kids >= 1 && nmbr_kids <3){
net_salary = base_salary + 200;
}
else if(nmbr_kids >= 3 && nmbr_kids <4){
net_salary = base_salary + 400;
}
else if (nmbr_kids >= 4){
net_salary = base_salary + 600;
}
else{
net_salary = base_salary;
}
/* now I want to account for the number of years worked by the employee and update accordingly his net_salary */
if(nmbr_years >= 1 && nmbr_years <3){
net_salary = net_salary + 200;
}
else if(nmbr_years >= 3 && nmbr_years <4){
net_salary = net_salary +400;
}
else if(nmbr_years >= 4){
net_salary = net_salary + 600;
}
else{
net_salary = net_salary;
}
Is there a better, more compact way to do the above? Or am I looking at the problem the wrong way?