+ 1
if with 2 conditions (or maybe more) combined
I don't know whether I'm not up to date or anything, I just knew that we can combined 2 (or maybe more) conditions in if e.g.: //normal commands int num = 3; if (0 < num && num < 5) cout << num << "!" << endl; //combined if (0 < num < 5) cout << "it worked! num is " << num << endl; is this new syntax in C++ or... I just didn't know it all this time?
14 Réponses
+ 9
I've tried to explain how your "combined" example works. I thought that you understand example with && operator, so I didn't put focus on it.
Anyway Diego compared them and explained it really nicely.
Glad it's all clear now :)
+ 8
I think it works like this:
num= 2;
if(0<num<5)
if(1<5)
that is true so you will see your output, but if you write 1 instead of 5 then:
if(1<1) is false and you will get no output.
In Java this is not possible. In C++ boolean values can be used in mathematical expressions, where true is 1, and false is equal to 0.
This may be useful:
https://stackoverflow.com/questions/5189072/c-bool-question
+ 5
Chipp Changed my answer. Found it a bit confusing.
They're not the same thing.
cout << (0 < 42 < 5) << endl; // True
cout << (0 < 42 && 42 < 5) << endl; // False
The first one is interpreted as
(0 < 42) < 5
(1) < 5 // True
The second one is interpreted as
(0 < 42) && (42 < 5)
(1) && (0) // False
+ 2
voja i don't undrstnd wht you mean...
Diego how could it resulted Normal: True ?
anyway, i tried to compile similar code in solo learn, but it doesn't work anymore... but i've done it in here http://ideone.com/2lf23Y
+ 2
"Compiler does not assume left to right evaluation. The operands are grouped by associativity rules which is left to right for < opetator"
And where that rule has been dictated to follow? Obviously by the language
and what creature is responsible for applying that rule and making sure it conforms the language standard? Obviously again, the compiler
+ 2
I see. Thanks for your time.
I also found this[1] page full of profound information especially this paragraph
" there is no concept of left-to-right or right-to-left evaluation in C++. This is not to be confused with left-to-right and right-to-left associativity of operators: the expression f1() + f2() + f3() is parsed as (f1() + f2()) + f3() due to left-to-right associativity of operator+, but the function call to f3 may be evaluated first, last, or between f1() or f2() at run time. "
which is very close to what you've mentioned.
[1] https://en.cppreference.com/w/cpp/language/eval_order
+ 1
"is this new syntax in C++"
Well, that would've been cute if something that obvious mathematically could've been adapted and implemented into language grammar. Anyway!
Although the result of both expression finally boils down to a single boolean entity which might be true or false, however as mentioned by others, they completely catch you off guard regardless.
The statement
if (0 < num && num < 5)
compare to
if (0 < num < 5)
is clearer in what it's supposed to accomplish since two different comparisons well-placed both sides of a well-defined logical binary operator which acts upon the result of them after initial evaluation.
`if (true && false)` which is `if (false)`
on the contrary, the second one only relies on the internal ordering by which multiple homogenous relational operators (that is, `<` the "less than..." operator) would being evaluated and eventually silently produce the (unexpected) result. Why?
Mathematically, the expression
0 < 42 < 5
is definitely wouldn't satisfy whatever evaluation it should be going through. So it's simply false. With this mindset, if all of a sudden something similar appear inside a conditional statement the first thing that comes to mind is an immediate recall to its original mathematical form. That's not true. Even the compiler issues a clear hint as
" comparisons like ‘X<=Y<=Z’ do not have their mathematical meaning "
to remind the programmer of what might going wrong. What really happens if we ignore the above message is the compiler while parsing the source code applies a left-to-right grouping. In other words,
(0 < 42) < 5
is the thing from the compiler and language point of view which surprisingly make a big difference.
+ 1
"There is no "assumption" there are predefined rules."
Totally right.
"Moreover associativity is compile time and evaluation is runtime."
Yes, I should've said "grouping" not "evaluation". Thanks, will fix that.
"Saying left to right evaluation means the "compiler evaluates" lhs operand first which is not true."
Here, by "compiler evaluates" it implies the same meaning of "grouping" as you pointed out?!
"Compiler implementors are free to decide whether to evalute lhs first or rhs first."
As far as the language standard concern, they couldn't easily decide that since the standard has explicitly mandated it as
"The relational operators group left-to-right. [...]"