Is it just simply Left to Right evaluation that brings about the short circuit with only b1 assigned?
Sergey Zhylinsky wrote:I don't understand... Another example:
The result of the preceeding code is 30. Assignment operators have the same precedence. We have two assignments operators, so we should execute them as per their associativity, i.e. from right to left. The first operator that is to be executed is '*='. Its result is 20. But then the other assignment operator '+=' adds 10 to 20. Why doesn't it add 20 to 20? The '*=', when have been executed first, must have been assigned a new value to the variable, mustn't it?
Sergey Zhylinsky wrote:The precedence of '&&' is higher than that of '||'. Therefore, it would be executed first. But order of evaluation is beyond the precedence and associativity. As the evaluation order comes from left to right, the first expression '(b1 = true)' will be evaluated first. As it is 'true' there is no need to evaluate other expressions.
Roel De Nijs wrote:
Sergey Zhylinsky wrote:The precedence of '&&' is higher than that of '||'. Therefore, it would be executed first. But order of evaluation is beyond the precedence and associativity. As the evaluation order comes from left to right, the first expression '(b1 = true)' will be evaluated first. As it is 'true' there is no need to evaluate other expressions.
Exactly!
Now what will the output be if || and && are swapped? What's the output of this code snippet?
Tony Singarayar wrote:Wouldn't line 3 be evaluated as per below.
1.(b1 = true) &&( (b2 = true) || (b3 = true))
OR
2.((b1 = true) && (b2 = true)) || (b3 = true); //as sergei mentioned...
Tony Singarayar wrote:1.If the expression's operands doesn't have brackets, the expression is evaluated based on precedence of operator.
2.If the expression involves brackets(if one of the operand has brackets), evaluation is done from left to right only.
3. Which means that first operand is evaluated first and then its operated with remaining expression. Operator precedence doesn't come into play here...
Tony Singarayar wrote:Since parentheses are takes priority over operator precedence, should parantheses-assignment((b1 = true),(b2 = true),(b3 = true)) be happening first.
Prathima gaitonde wrote:What does associativity means?
What is its order out of 3
a>Order of evaluation
b>Order of precedence
and c>Associativity?
Prathima gaitonde wrote:Hence final out put is b1=true, b2=true, b3=false, result=true.
Correct me if I am wrong.
Prathima gaitonde wrote:
2> Evaluation begins from left to right, Hence (((b1=true)&&(b2=true)) || (b3=true)), here b1=true first innermost bracket from left is evaluated then there is && operator, which is a short circuit, so as the b1=true it has to check for the value of b2 which is then assigned a value true. Now second innermost bracket from left results true, next operator is || as this short circuit operator looks for value true in the first place it wont evaluate value of b3, hence result is assigned a value true.
Hence final out put is b1=true, b2=true, b3=false, result=true.
Sharmili Rameshbabu wrote:I went through the entire discussion, still i dont understand one point that why should the evaluation actually happening is (((b1=true)&&(b2=true)) || (b3=true)), instead of (b1=true)&&((b2=true) || (b3=true))
In the second form also. && is given higher precedence and rest of the expression is considered as the second operand. Please advise
Sharmili Rameshbabu wrote:And a small request, Can you please reply to my post here
Trying to collect the broken pieces of my life,in the process of making out a beautiful picture out of it.
Sachin Tripathi wrote:
Where I am deviating from correct path,would love if you will point the exact step
Sachin Tripathi wrote:Where I am deviating from correct path,would love if you will point the exact step
Trying to collect the broken pieces of my life,in the process of making out a beautiful picture out of it.
Trying to collect the broken pieces of my life,in the process of making out a beautiful picture out of it.
Sachin Tripathi wrote:
The reason behind the answer which i gave:
Output:
Evaluation = precedence(R to L)
Trying to collect the broken pieces of my life,in the process of making out a beautiful picture out of it.
Sachin Tripathi wrote:Evaluation always occurs from L to R
But during evaluation if operator has less precedence then following operator then the final result of operation involving less precedence operator is given after operation involving higher precedence one is given(this is general case)
Sachin Tripathi wrote:But if operator has a property of short circuit and left operand compels short circuit then final result of operation involving less precedence operator can be given depending upon their position in their expression
Trying to collect the broken pieces of my life,in the process of making out a beautiful picture out of it.
Sachin Tripathi wrote:To make my point clear,consider op's first question
In which || has less precedence then &&
Even though due to short circuit property the result due to || is evaluated first without evaluating &&
Because it is the leftmost operator
Trying to collect the broken pieces of my life,in the process of making out a beautiful picture out of it.
Sunglasses. AKA Coolness prosthetic. This tiny ad doesn't need shades:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
|