简体   繁体   中英

Nesting of the conditional operator

I had to make a program wherein I was supposed to take age and experience (whether the employee is experienced or not) of an employee as input from the user and print out his/her salary. The salary was subject to the following conditions:

  • If employee is inexperienced, salary=2000 irrespective of age.
  • For an experienced employee:
    • If age<=28, salary=3000.
    • If 28<age<=35, salary=4800.
    • If age>35, salary=6000.

I made the following C++ program:

.
.
.
  cout<<"The salary of the employee is Rs."<<(experience?sal1:2000);  //LINE1
  sal1=((age<=28)?3000:sal2);
  sal2=((age<=35)?4800:6000);

  return 0;
}

where age , sal1 , sal2 are declared as int and experience as bool .

experience=1 is entered by user for experienced employee and otherwise experience=0 .

But whenever experience==1 and any age>28 is entered, I get unexpectedly large integral results whereas the code produces absolutely perfect results when conditional operator is nested.(ie I copy the expression of sal1 to the truth expression in LINE1 and copy the expression of sal2 into expression of sal1 )

Please explain what is the difference between both of these codes and why am I getting unexpected results in the first case.

NOTE : I have used the gcc g++ compiler for compilation of my code. Please tell if it's the fault of the compiler, the operator or is there any other issue.

I think it would be easier to understand the logic, both for writing and for reading, if you used plain if... else if... else instead.

Perhaps something like

int salary;
if (age <= 28)
{
    salary = 3000;
}
else if (age > 28 && age <= 35)
{
    salary = 4800;
}
else
{
    // Age must be over 35 to come here
    salary = 6000;
}

No need such a difficult to read (and write) code:

#include <iostream>

int calcSalary(int age, bool experience){
   if (!experience)
      return 2000;

   if (age <= 28)
      return 3000;

   if (age <= 35)
      return 4800;

   return 6000;
}

int main(){
   std::cout << "The salary of the employee is Rs." << calcSalary(36, false) << '\n';

   return 0;
}

Assuming C++11, you can use lambda:

#include <iostream>
    
int main(){
   auto calcSalary = [](int age, bool experience){
      if (!experience)
         return 2000;

      if (age <= 28)
         return 3000;

      if (age <= 35)
         return 4800;

      return 6000;
   };

   std::cout << "The salary of the employee is Rs." << calcSalary(36, false) << '\n';

   // no return 0 need for C++11
}

If you really want to use nested ternary operators (for instance if you want to use const everywhere), then you can do something like this:

auto const salary = (!isExperienced) ? 2000 :
                    (age <= 28 ) ? 3000 :
                    (age <= 35 ) ? 4800 : 6000;

remember that the ternary ( ? ) operator works like this:

(conditional-statement) ? (if-true) : (if-false)

when you chain them together like in the example, then they are evaluated sequentially from top to bottom. So testing for isExperience is first, and if the employee is NOT experienced then we evaluate as 2000 . From there we test if less than or equal to 28 , less than or equal to 35 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM