operator-precedence

Why does the Perl conditional operator not do what I expect?

This snippet of Perl code in my program is giving the wrong result. $condition ? $a = 2 : $a = 3 ; print $a; No matter what the value of $condition is, the output is always 3, how come? Edit: I wonder if Perl is alone in this regard. Does your favorite language suffer from this bug/feature ? ...

Priority of C++ operators "&" and "->"

Given the following: &row->count Would &(row->count) be evaluated or (&row)->count be evaluated in C++? EDIT: Here's a great link for C++ precedence. ...

C++ Mystery

Can someone explain to me why this code prints 14? I was just asked by another student and couldn't figure it out. int i = 5; i = ++i + ++i; cout<<i; ...

Should One Know Operator Precedence thoroughly?

Should the programmer be aware of operator precedence thoroughly? Using braces to group expressions should be okay, isn't? I always uses braces to be on safer side. And when asked a question on precedence, I cannot answer readily. ...

C #define macros

Hi. Here is what i have and I wonder how this works and what it actually does. #define NUM 5 #define FTIMES(x)(x*5) int main(void) { int j = 1; printf("%d %d\n", FTIMES(j+5), FTIMES((j+5))); } It produces two integers: 26 and 30. But i can't seem to understand how it makes this possible.. Thanks. ...

How do I parenthesize an expression programmatically?

I have an idea for a simple program to make that will help me with operator precedence in languages like C. The most difficult part of this is parenthesizing the expression. For example, I want this: *a.x++ = *b.x++ Converted to this: ((*(((a).(x))++)) = (*(((b).(x))++))) Which I did manually in these steps: *a.x++ = *b...

Prolog operator precedence and rules matching

I have the next two facts loaded in my prolog interpreter: foo(U+V,1). foo(U*V,2). Now I try the next queries with that results: foo(x*x+x,R). --> R = 1 foo(x+x*x,R). --> R = 1 foo(x*x*x,R). --> R = 2 Now I try with the next query: foo(x*x-x,R). --> no As I understand, this is explained by how the operator precedence bui...

Understanding evaluation of expressions containing '++' and '->' operators in C.

Consider this example: struct { int num; } s, *ps; s.num = 0; ps = &s; ++ps->num; printf("%d", s.num); /* Prints 1 */ It prints 1. So I understand that it is because according to operators precedence, -> is higher than ++, so the value ps->num (which is 0) is firstly fetched and then the ++ operator operates on it, so it incre...

OR Operator in c#

Can I achieve if(a == "b" || "c") instead of if(a == "b" || a== "c") ...

dereference and advance pointer in one statement?

I'm reading from a byte array as follows: int* i = (int*)p; id = *i; i++; correct me if I'm wrong, but ++ has precedence over *, so is possible to combine the *i and i++ in the same statement? (e.g. *i++) (this is technically unsafe C#, not C++, p is a byte*) ...

C# conditional AND (&&) OR (||) precedence

We get into unnecessary coding arguments at my work all-the-time. Today I asked if conditional AND (&&) or OR (||) had higher precedence. One of my coworkers insisted that they had the same precedence, I had doubts, so I looked it up. According to MSDN AND (&&) has higher precedence than OR (||). But, can you prove it to a skeptical co...

SQL Logic Operator Precedence: And and Or

Are the two statements below equivalent? SELECT [...] FROM [...] WHERE some_col in (1,2,3,4,5) AND some_other_expr and SELECT [...] FROM [...] WHERE some_col in (1,2,3) or some_col in (4,5) AND some_other_expr Is there some sort of truth table I could use to verify this? Thanks. ...

int[] arr={0}; int value = arr[arr[0]++]; Value = 1?

Today I came a cross an article by Eric Lippert where he was trying to clear the myth between the operators precedence and the order of evaluation. At the end there were two code snippets that got me confused, here is the first snippet: int[] arr = {0}; int value = arr[arr[0]++]; Now when I think about the value of the va...

Is there a difference on how java performs operations using shortcut operators from the regular ones?

I am working on a java program concerning the pascal's triangle. So this is how it is coded: for(int i = 0; i < 5; i++){ for(int j = 0, x = 1; j <= i; j++){ System.out.print(x + " "); x = x * (i - j) / (j + 1); } System.out.println(); } and it shows: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 But when I tried to...

PowerShell: how *exactly* does the RHS of the -f operator work?

Last time I got confused by the way PowerShell eagerly unrolls collections, Keith summarized its heuristic like so: Putting the results (an array) within a grouping expression (or subexpression e.g. $()) makes it eligible again for unrolling. I've taken that advice to heart, but still find myself unable to explain a few esoterica. ...

Member access differences.

can someone tell me what is the different between (*ptr).field and ptr->field? I know it connect somehow to static and dynamic linking, but i dont know what is it. can someone tell me the differnet and give me an example? edit: if i have this code: Point p; //point is a class that derive from class shape Shape *s=&p; ...

Which side (left or right) of && (and) operator evaluated in C++

Which order is the and && operator evaluated For example the following code if (int alpha = value1-value2 && alpha > 0.001) //do something threw an exception that alpha is being used without being initiated. I thought the expression left of the && would always initiate the value of alpha first, but it seems I may be wrong Any id...

Java operator precedence guidelines

Misunderstanding Java operator precedence is a source of frequently asked questions and subtle errors. I was intrigued to learn that even the Java Language Specification says, "It is recommended that code not rely crucially on this specification." JLS §15.7 Preferring clear to clever, are there any useful guidelines in this area? Here a...

In Java, what are the boolean "order of operations"?

Let's take a simple example of an object "Cat". I want to be sure the "not null" cat is either orange or grey. if(cat != null && cat.getColor() == "orange" || cat.getColor() == "grey") { //do stuff } I believe AND comes first, then the OR. I'm kinda fuzzy though, so here are my questions: 1) Can someone walk me through this state...

Are there Ruby precedence issues with using Proc.call vs. Proc.[]?

Recently I was having a discussion with a friend about Ruby's Proc. You can call a Proc in one of several ways. One way is to invoke Proc.call: p = Proc.new { |x| "hello, #{x}" } p.call "Bob" => "hello, Bob" Another is to use braces, Proc.[]: p ["Bob"] => "hello, Bob" Are there any potential precedence issues here, or are these two...