operator-precedence

Why is x == (x = y) not the same as (x = y) == x?

淺唱寂寞╮ 提交于 2019-12-02 15:44:51
Consider the following example: class Quirky { public static void main(String[] args) { int x = 1; int y = 3; System.out.println(x == (x = y)); // false x = 1; // reset System.out.println((x = y) == x); // true } } I'm not sure if there is an item in the Java Language Specification that dictates loading the previous value of a variable for comparison with the right side ( x = y ) which, by the order implied by brackets, should be calculated first. Why does the first expression evaluate to false , but the second evaluate to true ? I would have expected (x = y) to be evaluated first, and then it

String concatenation and comparison gives unexpected result in println statement

旧巷老猫 提交于 2019-12-02 14:01:11
I couldn't figure out the following behaviour, String str1= "abc"; String str2 = "abc"; System.out.println("str1==str2 "+ str1==str2); System.out.println("str1==str2 " + (str1==str2)) Output for the above statement is as follows: false str1==str2 true Why is this happening? Why the output is not like follows: str1==str2 true str1==str2 true TheLostMind + has higher precedence than == . So your code : System.out.println("str1==str2 " + str1 == str2); will effectively be System.out.println(("str1==str2 "+str1) == str2); so, you get false . In case-2 System.out.println("str1==str2 " + (str1==str2

Why does i|= j|= k|= (j+= i) - - (k+++k) - - (i =+j) == 11?

怎甘沉沦 提交于 2019-12-02 13:48:24
I came across this code in a project I have started working on. The original developer is no longer available, and I can't make any sense of it: k = (j = (i = 0) + 2) + 1; return i|= j|= k|= (j+= i) - - (k+++k) - - (i =+j); It produces a value of 11 . How does this work? What is the =+ operator? What is the +++ operator? What is the - - operator? What is the |= operator? What is the =+ operator? That's two operators, one assignment operator, = , and one unary plus, + , which does nothing. Did you typo and mean the compund assignment operator += ? What is the +++ operator? Also two operators,

Operator precedence and ternary operator

ぃ、小莉子 提交于 2019-12-02 11:39:24
问题 I have a problem in C. #include<stdio.h> int main() { int a = 10, b = 0, c = 7; if (a ? b : c == 0) printf("1"); else if (c = c || a && b) printf("2"); return 0; } This code prints 2 but I think a?b:c returns b=0 and 0==0 returns 1. Can you explain the code? 回答1: Your conditions are not properly written. In the first if-statement: if (a ? b : c == 0) if you put the values, then it becomes if(10 ? 0 : 7 == 0) means, it will always return 0. That's why control goes to the else part and there,

c function parameters order of evaluation

让人想犯罪 __ 提交于 2019-12-02 10:34:59
问题 I understand that there are no guarantees regarding the order which the parameters of a function will be called, but, isn't it guaranteed that if there's a function call as parameter, that function will be called first? I help students on the laboratory of an introductory subject on programming, and they were supposed to create a recursive factorial function which receives the n (for n!) and a pointer to an integer that will be used to count function calls and then they are supposed to print

Evaluation of the following expression

女生的网名这么多〃 提交于 2019-12-02 09:48:16
The following code snippet: int i=-3,j=2,k=0,m; m=++i && ++j || ++k; can be evaluated using two concepts,I believe: 1.Since ++ operator has greater precedence than the logical operators,so first all increment operators will be evaluted,then && having higher precedence than || will be computed.In this process,k will be incremented. 2.First && operator will be evaluated.For this ++ i and ++j will be computed.Since the result of the && operator is 1,no need to evaluate the ++k.So k will not be incremented. When I try it on a system, the result proves reasoning 2 to be correct and 1 to be wrong.

PHP Logical Operators precedence affects variable assignment results strangely

北慕城南 提交于 2019-12-02 09:08:30
$var4 = 123; function fn1($p1) { return array('p1' => 1, 'p2' => 2); } if ($var1 = fn1(1) AND $var4 == 123) { print_r($var1); } if ($var2 = fn1(1) && $var4 == 123) { print_r($var2); } if (($var3 = fn1(1)) && $var4 == 123) { print_r($var3); } If you run this simple script it will output strange results, at least for me!! First output from first if expression will result in an array returned from the function & assigned to the $var1 variable, which is what I'm expecting, well? Second output from second if expression will result in an integer '1' assigned to the $var2 variable, which is NOT

Confusing operator precedence: a << b + c << d

冷暖自知 提交于 2019-12-02 06:50:16
问题 Operator + has higher precedence than << in C++, which would mean that expression a << b + c << d should be evaluated as: a << (b + c) << d But that does not make sense . More sense can be obtained when following a << (b + (c << d)) But that violates precedence of the + over << , doesn't it? How do compilers evaluate the "does makes sense" part? UPDATE: When asking the question I thought that second variant is used by the compiler, which is why I wondering how did the compiler come to this

How can I understand nested ?: operators in PHP? [duplicate]

ε祈祈猫儿з 提交于 2019-12-02 04:59:28
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Problem with PHP ternary operator I was reading up a bit on PHP in this article, and I stopped for a while to consider one of his gripes. I can't figure out how on earth PHP comes to the result that it does. Unlike (literally!) every other language with a similar operator, ?: is left associative. So this: $arg = 'T'; $vehicle = ( ( $arg == 'B' ) ? 'bus' : ( $arg == 'A' ) ? 'airplane' : ( $arg == 'T' ) ? 'train'

Operator precedence and ternary operator

狂风中的少年 提交于 2019-12-02 04:45:36
I have a problem in C. #include<stdio.h> int main() { int a = 10, b = 0, c = 7; if (a ? b : c == 0) printf("1"); else if (c = c || a && b) printf("2"); return 0; } This code prints 2 but I think a?b:c returns b=0 and 0==0 returns 1. Can you explain the code? Nishu Tayal Your conditions are not properly written. In the first if-statement: if (a ? b : c == 0) if you put the values, then it becomes if(10 ? 0 : 7 == 0) means, it will always return 0. That's why control goes to the else part and there, it becomes else if (7 = 7 || 10 && 0) since you used the "=" operator here (c = c), it will be