operator-precedence

Do parentheses force order of evaluation and make an undefined expression defined?

送分小仙女□ 提交于 2019-11-29 01:35:20
I was just going though my text book when I came across this question What would be the value of a after the following expression ? Assume the initial value of a = 5.Mention the steps a+=(a++)+(++a) At first I thought this is undefined behaviour because a has been modified more than once. So then I read the question and it said Mention the steps so I probably thought this question is right. So my question is : Does applying parentheses make an undefined behaviour defined ? Is a sequence point created after evaluating a parentheses expression ? If it is defined,how does the parentheses matter

Why does “new Date().toString()” work given Javascript operator precedence?

倾然丶 夕夏残阳落幕 提交于 2019-11-29 00:32:07
问题 MDN states that there are two operators in Javscript that share the highest precedence: The left-associative member operator: foo.bar The right-associative new operator: new Foo() I usually explicitly separate the two: (new Date()).toString() But I frequently see both of them combined: new Date().toString() According to this answer, the reason the second way works is that it's the second operator's associativity that matters when both operators have equal precedence. In this case, the member

Multiple assignment confusion

故事扮演 提交于 2019-11-28 23:23:05
I understand that the assignment operator is right associative. So for example x = y = z = 2 is equivalent to (x = (y = (z = 2))) That being the case, I tried the following: foo.x = foo = {a:1} I expected that the object foo would be created with value {a:1} and then the property x will be created on foo which will just be a reference to the foo object. (This is actually what happens if I was to separate the multiple assignment statement into two separate statements foo = {a:1};foo.x = foo; ) The outcome was actually: ReferenceError: foo is not defined(…) So then I tried the following: var foo

JS vs PHP: assignment operator precedence when used with logical-or

亡梦爱人 提交于 2019-11-28 21:25:14
(PHP has || and OR . JS only has || .) JS. According to MDN || has higher precedence than = . So this doesn't work: a || a = 1; because it's evaluated as: (a || a) = 1; which results in an "Invalid left-hand side in assignment". I understand that. That makes sense. PHP. According to PHP.net it works the same for PHP: || before = . However, I use this all the time: $a || $a = 1; Why does it work in PHP?? And to top it off: PHP's OR has lower precedence than = , so these shouldn't do the same: $a || $a = 1; $a OR $a = 1; but they do... https://3v4l.org/UWXMd I think JS' || works according to MDN

Calling a method on a new object in Java without parentheses: order of operations violation?

大兔子大兔子 提交于 2019-11-28 18:39:21
According to this table of Java operator precedence and associativity , member access has higher precedence than the new operator. However, given a class myClass and a non-static member function myFunction , the following line of code is valid: new myClass().myFunction(); If . is evaluated before new , how can this line be executed? In other words, why aren't parentheses required? (new myClass()).myFunction(); My guess is that since () shares precedence with . , the myClass() is evaluated first, and so the compiler knows even before evaluating the new keyword that the myClass constructor with

A strange operation problem in SQL Server (-100/-100*10 = 0)

坚强是说给别人听的谎言 提交于 2019-11-28 16:16:01
问题 If you execute SELECT -100/-100*10 the result is 0 . If you execute SELECT (-100/-100)*10 the result is 10 . If you execute SELECT -100/(-100*10) the result is 0 . If you execute SELECT 100/100*10 the result is 10 . BOL states: When two operators in an expression have the same operator precedence level, they are evaluated left to right based on their position in the expression. And Level Operators 1 ~ (Bitwise NOT) 2 * (Multiplication), / (Division), % (Modulus) 3 + (Positive), - (Negative),

Boolean Expression - Order of Operations

大兔子大兔子 提交于 2019-11-28 14:14:48
I have a test in Excel VBA: If (test1) And (test2) And (test3) Then 'do something End If In C, Java, etc. test1 would be run first, then test2, then test3. Critically, if test1 is false the whole test is false so the remaining tests do not run. Does that happen in this case with VBA? If so, in which order are the tests running? In all VBs prior to .NET there is no such thing as short-circuit. All expressions will be evaluated even if not required. If you want short-curcuit, do nested IFs. 来源: https://stackoverflow.com/questions/345399/boolean-expression-order-of-operations

Why does the “or” go before the “and”?

↘锁芯ラ 提交于 2019-11-28 13:51:45
int it=9, at=9; if(it>4 || ++at>10 && it>0) { System.out.print("stuff"); } System.out.print(at); prints out stuff9 and I want to know why as I thought ++at>10 && it>0 would be evaluated first and thus make at = 10. Operator precedence only controls argument grouping; it has no effect on execution order. In almost all cases, the rules of Java say that statements are executed from left to right. The precedence of || and && causes the if control expression to be evaluated as it>4 || (++at>10 && it>0) but the higher precedence of && does not mean that the && gets evaluated first. Instead, it>4 is

C/C++ Math Order of Operation

我只是一个虾纸丫 提交于 2019-11-28 13:47:39
So I know that C++ has an Operator Precedence and that int x = ++i + i++; is undefined because pre++ and post++ are at the same level and thus there is no way to tell which one will get calculated first. But what I was wondering is if int i = 1/2/3; is undefined. The reason I ask is because there are multiple ways to look at that (1/2)/3 OR 1/(2/3). My guess is that it is a undefined behavior but I would like to confirm it. jcoder In your example the compiler is free to evaluate "1" "2" and "3" in any order it likes, and then apply the divisions left to right. It's the same for the i++ + i++

Precedence: Logical or vs. Ternary operator

时光总嘲笑我的痴心妄想 提交于 2019-11-28 12:59:27
Consider the following: (EDIT: I've amended the function slightly to remove the use or braces with the ternary operator) function someFunction(start,end,step){ var start = start || 1, end = end || 100, boolEndBigger = (start < end); // define Boolean here step = step || boolEndBigger ? 1:-1; console.log(step); } someFunction() // step isn't defined so expect (1<10) ? 1:-1 to evaluate to 1 someFunction(1,10) // again step isn't defined so expect to log 1 as before The problem: someFunction(1,10,2) //step IS defined, shortcut logical OR || should kick in, //step should return 2 BUT it returns 1