operator-precedence

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

ぐ巨炮叔叔 提交于 2019-11-27 13:49:51
问题 (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:

Is this code well-defined?

对着背影说爱祢 提交于 2019-11-27 13:30:49
This code is taken from a discussion going on here . someInstance.Fun(++k).Gun(10).Sun(k).Tun(); Is this code well-defined? Is ++k in Fun() evaluated before k in Sun()? What if k is user-defined type, not built-in type? And in what ways the above function calls order is different from this: eat(++k);drink(10);sleep(k); As far as I know, in both situations, there exists a sequence point after each function call . If so, then why can't the first case is also well-defined like the second one? Section 1.9.17 of the C++ ISO standard says this about sequence points and function evaluation: When

Order of execution of parameters guarantees in Java?

吃可爱长大的小学妹 提交于 2019-11-27 13:24:05
Given the following function call in C : fooFunc( barFunc(), bazFunc() ); The order of execution of barFunc and BazFunc is not specified, so barFunc() may be called before bazFunc() or bazFunc() before barFunc() in C . Does Java specify an order of execution of function argument expressions or like C is that unspecified? Michael Easter From the Java Language Specification (on Expressions): 15.7.4 Argument Lists are Evaluated Left-to-Right In a method or constructor invocation or class instance creation expression, argument expressions may appear within the parentheses, separated by commas.

Ternary conditional and assignment operator precedence

蹲街弑〆低调 提交于 2019-11-27 13:05:20
I'm confused about direct assignment and ternary conditional operators precedence: #include<stdio.h> int main(void) { int j, k; j = k = 0; (1 ? j : k) = 1; // first printf("%d %d\n", j, k); j = k = 0; 1 ? j : k = 1; // second printf("%d %d\n", j, k); return 0; } I would expect the output to be: 1 0 1 0 But it happens to be: 1 0 0 0 Plus I get this warning: main.cpp:20: warning: statement has no effect which is about the line I commented as second. Since the direct assignment operator has less precedence than the ternary conditional operator, I was expecting lines commented as first and second

In what order does a C# for each loop iterate over a List<T>?

依然范特西╮ 提交于 2019-11-27 12:58:08
I was wondering about the order that a foreach loop in C# loops through a System.Collections.Generic.List<T> object. I found another question about the same topic, but I do not feel that it answers my question to my satisfaction. Someone states that no order is defined. But as someone else states, the order it traverses an array is fixed (from 0 to Length-1). 8.8.4 The foreach statement It was also said that the same holds for any standard classes with an order (e.g. List<T> ). I can not find any documentation to back that up. So for all I know it might work like that now, but maybe in the

Haskell operator vs function precedence

岁酱吖の 提交于 2019-11-27 12:35:54
I am trying to verify something for myself about operator and function precedence in Haskell. For instance, the following code list = map foo $ xs can be rewritten as list = (map foo) $ (xs) and will eventually be list = map foo xs My question used to be, why the first formulation would not be rewritten as list = (map foo $) xs since function precedence is always higher than operator precedence, but I think that I have found the answer: operators are simply not allowed to be arguments of functions (except of course, if you surround them with parentheses). Is this right? If so, I find it odd,

Is it safe to rely on Python function arguments evaluation order? [duplicate]

喜欢而已 提交于 2019-11-27 09:24:10
This question already has an answer here: Is Python's order of evaluation of function arguments and operands deterministic (+ where is it documented)? 2 answers Is it safe to assume that function arguments are evaluated from left to right in Python? Reference states that it happens that way but perhaps there is some way to change this order which may break my code. What I want to do is to add time stamp for function call: l = [] l.append(f(), time.time()) I understand that I can evaluate the arguments sequentially: l = [] res = f() t = time.time() l.append(res, t) But it looks less elegant so

Are equal timeouts executed in order in Javascript?

拜拜、爱过 提交于 2019-11-27 08:16:02
Suppose I do setTimeout(foo, 0); ... setTimeout(bar, 0); Can I be sure foo will begin executing before bar? What if instead of 0 I use a timeout of 1, 10, or 100 for bar? Simple experiments show that in the case of equal timeout values the timeout targets are executed in the same order as the setTimeouts themselves, but is it safe to rely on this behavior? It is not safe to rely on this behavior. I wrote a test script which schedules a number of functions (and they in turn also schedule a number of functions) all using setTimeout(..., 0), and the order in which the functions were called was

Precedence: Logical or vs. Ternary operator

随声附和 提交于 2019-11-27 07:21:45
问题 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

Order of evaluation of expression

≯℡__Kan透↙ 提交于 2019-11-27 07:09:05
问题 I've just read that order of evaluation and precedence of operators are different but related concepts in C++. But I'm still unclear how those are different but related?. int x = c + a * b; // 31 int y = (c + a) * b; // 36 What does the above statements has to with order of evaluation. e.g. when I say (c + a) am I changing the order of evaluation of expression by changing its precedence? 回答1: The important part about order of evaluation is whether any of the components have side effects.