if-else to ternary conversion – Computer Sir Ki Class

Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  

Login
[lwa]



Exam Questions-ICSE2018-03I #JAVA#4069    siteicon   siteicon  

Problem Statement - if-else to ternary conversion

Rewrite the following using ternary operator:
if (bill>10000)
discount=bill*10.0/100;
else
discount = bill*5.0/100;

Solution

TC++ #4069

The given expression using ternary operator can be written as:

discount=(bill>10000) ? (bill*10.0/100) : (bill*5.0/100);

Notes

  • The given solution will work well without putting brackets also, but for sake of clarity it is always good to put brackets in ternary operator sub-expressions.


Share

sunmitra| Created: 25-Mar-2018 | Updated: 12-Apr-2019|ICSE2018






Back