# Thread: Nice use for the conditonal operaror

1. ## Nice use for the conditonal operaror

Hi I came across a nice use for conditional operator. I always hated dealing text for "You have 1 units" or "You have 2 units" - ie, dealing with plural for single items.

Code:
```int main(void)
{
const double unit_price = 3.50;   /* unit price in euro */
const double discount1 = 0.05;    /* discount for more than 10 */
const double discount2 = 0.10;    /* discount for more than 20 */
const double discount3 = 0.15;    /* discount for more than 50 */
double total_price = 0.0;
int quantity = 0;

printf("\nWelcome to the shop. Basic unit costs &#37;.2f. Discounts available.",unit_price);
printf("\n5%% off on more than 10. 10%% off on more than 20. 15%% off on more than 50!");
printf("\nEnter the number of units you want to buy: ");
scanf("%d",&quantity);

total_price = quantity*unit_price*(1.0 -
(quantity>50 ? discount3 : (
quantity>20 ? discount2 : (
quantity>10 ? discount1 : 0.0))));

printf("The price for %d unit%s is %.2f euro.\n", quantity, quantity>1 ? "s" : "", total_price);
/* quantity>1 check shows how to deal single or plural units */
return 0;
}```
The code segment:
Code:
`quantity, quantity>1 ? "s" : ""`
is pretty cool. It's one in my bookmarks. I love that! Going back I always neglected the shorthand conditional op.

Welcome to the shop. Basic unit costs 3.50. Discounts available.
5% off on more than 10. 10% off on more than 20. 15% off on more than 50!
Enter the number of units you want to buy: 1
The price for 1 unit is 3.50 euro.

2. in printf it increases readability...
but in total_price calculations - I prefer if/else for readability...
Code:
```coeff = 1.0;
if(quantity>50 ) coeff -= discount3 ;
else if (quantity>20 ) coeff -= discount2;
else if (quantity>10 ) coeff -= discount1;

total_price = quantity*unit_price*coeff;```
for me is a lot more easier to understand than the original code

3. Actually I agree with you vart. Though I do like the single/plural trick.

Code:
`quantity>1 ? "s" : ""`
The nested discount example above in the main code reminds me of that cursed nested DECODE statement in PLSQL where I come across it a lot - it's usually subnested to the high heavens!

The simple plural example at the end is nice though I must admit.

4. Keep in mind that 0 should also be plural, but your check is for >1. 1 is the only case in which it is not plural.

0 dollars
1 dollar
2 dollars

5. Also bear in mind, that if you come to translate this code into other languages, a simple search for all strings isn't going to work so well.

6. Originally Posted by Salem
Also bear in mind, that if you come to translate this code into other languages, a simple search for all strings isn't going to work so well.
That is problem is close to heart sometimes, I'm dealing with 2-3 byte Kanji characters in some rather sticky invoices of late

7. Originally Posted by itsme86
Keep in mind that 0 should also be plural, but your check is for >1. 1 is the only case in which it is not plural.

0 dollars
1 dollar
2 dollars
Thanks! I want to hire you as my code tester!

8. as well as 21 dollar should be singular

9. euros vs euro..... in Euroland - most folk say "5 euro" and a few say "5 euros". All I notice is that I get less for my euro!