Why does if ( 'a'<= /*some char*/ <= 'z') not work?
I have been taking an online course in C# and, while solving one of the tasks I came across this:
I needed to write a piece of code which determined if the first letter of a word is found between 'A' and 'F', so I wrote the following if statement:
string command= Console.ReadLine(); if (('a' <= command <= 'f') || ('A' <= command <= 'F'))
And gave me the following error: "Operator '<=' cannot be applied to operands of type 'bool' and 'char'"
So, after a lot of head scratching I replaced it with this:
if (('a' <= command && command <= 'f') || ('A' <= command && command <= 'F'))
And that worked just fine, but I can't figure out why the first one didn't work. Can someone explain this to me please?
If you split
'a' <= command <= 'f'
into its components (as the compiler would see it), you get
Stage 1: 'a' <= command Stage 2: (result of stage 1) <= 'f'
So, the 2nd half of the original statement becomes:
true <= 'f'
which is obviously the error you are seeing
Does Intermittent Fasting Work for Weight Loss?, Unlike diets and other weight loss programs, it doesn't restrict your food choices or intake. Instead, all that matters is when you eat. While� Why do we keep confessing our sins if all our sins have been canceled in Christ? It’s a great question from a listener to the podcast named Andy, who represents a lot of listeners out there asking this very same question.
The compiler interprets
'a' <= command <= 'f'
as if it were written like
('a' <= command) <= 'f'
Intermittent fasting: Surprising update, But many people find it too hard to get through the fasting interval. But why does simply changing the timing of our meals to allow for fasting� Why do even completely illiterate persons, who speak their national language poorly, speak their local dialect with perfection? 70s British TV show about 3 people who gained telepathic abilities and solved mysteries
In most languages you are not able to chain comparisons like that in a boolean expression.
The second works because you are breaking it down into one comparison at a time.
a < b
a < b && a < c && b < c
a < b < c
16:8 intermittent fasting: Benefits, how-to, and tips, If you buy through links on this page, we may earn a small commission. about the 16:8 intermittent fasting plan, including how to do it and the� Why Does God Allow So Much Suffering in the World? This is a question that has baffled people for generations and I am going to give you a great theological answer right now: I don’t know.
Intermittent fasting for weight loss: 5 tips to start, It does not matter what days a person fasts or even when they begin. The only restriction is fasting must last for 24 hours and on non-consecutive� For the same reason a good cashier will ask you if you desperately need a plastic bag for your single Slim Jim at the gas station rather than just giving you one: because most people don’t want a receipt for something that’s going to disappear sho
Why does sex hurt?, If you get pain during or after sex, your body may be trying to tell you something is wrong, so don't ignore it. See your GP or go to a sexual health (genitourinary� Why do people cheat? We'll break down what experts believe are the most common answers to this age-old question and offer tips for moving past infidelity.
Nipples are sensitive, and they can hurt for lots of reasons. Tight clothes, rashes, and infections can all irritate the tender skin.For women, sore nipples are common during periods, pregnancy