if statement with multiple conditions instead of using AND

  • A+
Category:Languages

I been searching for some information about if statements with multiple conditions but haven't found something that corresponds to myquestion.

I was wondering if you could write:

int n = 3 if (3 < n < 20) {    //do something.. } 

rather than doing:

if (n > 3 && n < 20) {    //do something.. } 

The first statement doesn't work for me which i think it should, because it's plain simple.

Maybe someone can give me the correct syntax for doing so or maybe it's just impossible at all and i just have to go with the AND.


To explain why it's invalid:

if (3 < n < 20) 

Could be rewritten as:

if ((3 < n) < 20) 

Now 3 < n's outcome would be a bool.

So basically you'll get:

if (true/false < 20) 

Which is not valid in C#.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: