G Gr
G Gr

Reputation: 6080

&& operator for double

I need abit of help with an if statement and operators. How could I do this:

double AgePenalty = 0;
if (AgeOfCustomer <= 21)
{
    AgePenalty = 15;
}
if (AgeOfCustomer <= 30 && AgeOfCustomer => 21) // cant use && operator with double
{
    AgePenalty = 10;
}

This just says if the customer is younger than 21 apply a certain price tag; if the customer is between the ages of 21 and 25 apply smaller price tag etc.

Upvotes: 2

Views: 820

Answers (3)

Narendra
Narendra

Reputation: 3117

Better write in the following way

double AgePenalty = 0;
        if (AgeOfCustomer <= 21)
        {
            AgePenalty = 15;
        }
        else if (AgeOfCustomer <= 30) // as check is done for 21 already.
        {
            AgePenalty = 10;
        }

The above code is little optimized.

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1499940

It's not && that's the problem - it's your greater-than-or-equal-to operator, which is >=, not =>.

if (AgeOfCustomer <= 30 && AgeOfCustomer >= 21)

=> is used for lambda expressions.

(It's not clear why you thought this was related to doubles particularly...)

Upvotes: 7

Habib
Habib

Reputation: 223207

Your check AgeOfCustomer => 21 is wrong, it should be : AgeOfCustomer >= 21 Just change your if statement to

  if (AgeOfCustomer <= 30 && AgeOfCustomer >= 21)

Upvotes: 10

Related Questions