Questions tagged [operator-precedence]
Operator Precedence refers to the rules governing the order in which operators are evaluated within an expression or statement in a programming language. Distinct from [order-of-execution] which covers the sequencing of execution events in a software system.
1,733
questions
0
votes
0
answers
17
views
Place parentheses in an expression
I have an expression like
1 L1 2 L2 3 R3 4 R2 5 L2 6 R1 7
where L1, L2, R1, R2 are operators.
The first letter indicates the associativity of the operator (left or right), and the number indicates its ...
0
votes
2
answers
35
views
PHP Null Coalesce combined with ternary has unexpected results
This is tested against PHP 7.4
I'm trying to understand why the first example does not return 'hello' like the other two. Example 3 is the same thing as example 1 except the ternary is encapsulated in ...
-1
votes
2
answers
46
views
How is x += 1 <= y evaluated in Python? [closed]
How is x += 1 <= y evaluated in Python?
Intuitively I assume it is x += (1 <= 2) but it seems to be (x += 1) <= 2, however without any return value and x += 1 returning the value of x before ...
0
votes
0
answers
34
views
Operator precedence in typescript shorthand array notation: typeof unknown[]
In puppeteer's declaration file types.d.ts, I came across the following type definition:
export declare type EvaluateFunc<T extends unknown[]>
= (...params: InnerParams<T>) => ...
-1
votes
0
answers
34
views
In this java code & operator has high precedence so first b > 5 & c++ < 10 will be evaaluated, then a> 5 && will be evaluated.. So output should be 9
class First {
public static void main(String[] args) {
int a=4, b=6, c=8;
boolean d = a>5 && (b>5 & c++<10);
System.out.println(c);
}
}
//In this java code ...
-3
votes
0
answers
17
views
Operator : Post increment operator output [duplicate]
//ITS A c++ Program, WELL IM confused about the output
#include <bits/stdc++.h>
using namespace std;
main()
{
int a, b;
b = 10;
a = ++b + ++b;
cout<<a<...
0
votes
0
answers
37
views
Why is this expression "while (b++ < 99)" in c programming evaluated a relational operator first before post-increment? [duplicate]
Since I assume that post increment is evaluated first before a relational operator, it will not going inside the second while loop when b=98 under the expression "while (b++ < 99)" in C ...
6
votes
1
answer
81
views
Priority of tuple (un)packing with inline if-else
Apologies in advance for the obscure title. I wasn't sure how to phrase what I encountered.
Imagine that you have a title of a book alongside its author, separated by -, in a variable title_author. ...
4
votes
3
answers
156
views
Equivalent of Haskell's $ operator in OCaml
Is there an equivalent to Haskell's $ operator in OCaml, or do I have to rely on brackets? See for example,
multiplyByFive 5 + 1 = 26
but
multiplyByFive $ 5 + 1 = 30
1
vote
0
answers
20
views
Python precedence 1 < 0 == 0 --> False [duplicate]
Why this expression evaluates to False.
print(1 < 0 == 0)
You would think that the expression on the left is evaluated first and then the expression on the right, thus returning True
Thanks!!
3
votes
1
answer
62
views
Confusion about operator precedence in C
I got bit confused by how to interpret the precedence of operators in the following snippet:
int a,b,c,d;
a=b=c=d=1;
a=++b>1 || ++c>1 && ++d>1
The values of a,b,c,d at the end of ...
1
vote
1
answer
22
views
Prevent JavaScript from overwriting identical embedded data in Qualtrics
I used JavaScript to assign the same embedded data name (QItem) to 5 survey items.
For example, QID1:
Qualtrics.SurveyEngine.addOnload(function()
{
var itemText = "${q://QID1/QuestionText}&...
1
vote
1
answer
50
views
Unexpected behavior of c++ program on executing two equivalent statements
I was trying to solve this problem,
while doing so,
it looks like
for (int i=row1; i<=row2; i++) {
if (col1 != 0) sum -= mat[i][col1-1];
sum += mat[i][col2];
}
and
for (int i=row1; i<=...
0
votes
2
answers
41
views
C++ Not getting expected result when executing a Macro
#include <stdio.h>
#define swapOut(a,b) a+b-a, a+b-b
int main()
{
int a = 5;
int b = 6;
printf("%d,%d", swapOut(a+b,b-a));
return 0;
}
When executing this ...
0
votes
1
answer
57
views
Parens required or Ruby says: TypeError (class or module required) ... why?
I'm trying this in irb, Ruby version 3:
3.0.0 :001 > num = 42
=> 42
3.0.0 :002 > num.is_a?(Integer) && num > 10
=> true
3.0.0 :003 > num.is_a? Integer && num > ...
2
votes
1
answer
73
views
Fortran user defined operators precedence rules
The built-in operators of Fortran have well-defined precedence rules. What about user-defined operators? I understand that user-defined binary operators have a lower priority than every built-in ...
-1
votes
1
answer
82
views
Why are the precedence and associativity rules different in C and Java? [duplicate]
I found that the precedence and associativity rules are different in C, C++ and Java.
Have a look at this code snippet:
#include<stdio.h>
void main(){
int k = 5;
int x = ++k*k--*4;
...
-2
votes
2
answers
139
views
List of the Prelude haskell operators, with precedence level
Where can I find a list of the infix Haskell operators in Prelude and base package in general, along with their precedence level, and whether they're right or left associative? I know this information ...
0
votes
0
answers
50
views
Visual Basic Order of Operations
I'm trying to rewrite a legacy Visual Basic app into a web app, and I've been going through the math of it and I've come across an oddity that I can't quite figure out:
return (x) * 1 / y
Now, from ...
0
votes
1
answer
37
views
Tableau : set as a context filter, order of operations?
What it the order of operation when you place a set in the filter shelf and promote it as a context filter ? Is my set context filter applied before any fixed LOD calculations for instance ?
...
2
votes
1
answer
38
views
void insertion with realloc()
I am trying to do a void insertion(), but always get a segmentation fault, see following. Same time, I referenced this link.
First, I did realloc(), then move every memory to the next space after the ...
1
vote
1
answer
71
views
Operator precedence for logical AND (&& )and logical OR (||)
As per the operator precedence table for JavaScript, I can see that && has higher precedence than ||.
So, for the following code snippet:
let x, y;
let z = 5 || (x = false) && (y = ...
0
votes
1
answer
33
views
(Seemingly) nonsensical operator precedence when accessing an array member of struct
A bit of an embarrassing question but I'm confused about operator precedence here:
typedef struct {
char arr[1024];
} SomeStruct;
mySomeStruct.arr[42]
Since array access has a higher operator ...
0
votes
0
answers
35
views
Trying to implement Bedmas functionality into my code
So I am basically trying to create an intermediate code generation in java. My code will take in user input and will try to print out the intermediate code generation representation of the code for ...
0
votes
0
answers
21
views
The following code giving answer 21. I am not understanding how the operators are working
#include<stdio.h>
int main()
{
int i = 10;
int d = ++i + i--;
printf ("d = %d\n",d);
return 0;
}
This code printing d = 21. The order of evaluation is taken right to left ...
0
votes
2
answers
85
views
Operator precedence - increment versus member access
Consider this:
++iterator->some_value
Will the iterator be forwarded before some_value is accessed?
According to cppreference increment and member access both have the same precedence of 2. Does ...
-1
votes
1
answer
58
views
How to understand this C operators question?
I just started to learn C programming with books, and I found a part of the code shown below. When I run this code, the result was another value (2008000) instead of 0xffff.
int i = 0;
unsigned int ...
0
votes
0
answers
47
views
Precedence of function call in C++ and Python [duplicate]
I have two similar codes in C++ and Python.
C++ Code
#include <iostream>
using namespace std;
int i = 0;
int fcn()
{
i = 1;
return 1;
}
int main()
{
i = 0;
i = i + fcn();
...
0
votes
0
answers
14
views
Regarding Order of Precedence and Associativity (C++) [duplicate]
//C++ Program to print diamond using stars
//N.B. The following code is correct and prints a diamond using stars.
//My doubt is regarding this expression : ***int Space=(2*n-1)/2;***
#include <...
0
votes
0
answers
57
views
Unary operator precedence in Operator Precedence Parser
I am implementing a Recursive Descent Parser to parse a C-like language. For parsing expressions, I was using an Operator Precedence parser, but I wanted to make the precedence of some binary ...
0
votes
0
answers
21
views
Overriding operator precedence with parentheses (unary and assignments) [duplicate]
I know that in java there is an operator hierarchy, and if you'd like one operator to act before another, you place a set of parentheses around the operator and its operand(s). However, when I try ...
0
votes
2
answers
66
views
Misunderstood Operators Precedence in C
I was doing some exercises on the order of execution of operations in C and I came across a case that I did not quite understand.
int d = 1;
int e = d--/10; // the result of e will be 0;
before ...
0
votes
2
answers
35
views
Seg fault after failing to loop multiple times in C
I am doing an exercise where I create an array and populate the elements with integers. I first input the length of the array to malloc for the size. Then, I scan the elements of each point in the ...
0
votes
0
answers
22
views
Setting With Copy Warning - What does it mean?
I am coding a simple solar panel system for a uni assignment. I am new to python so forgive me if this is an inappropriate way to index/run the for loop. This is my function which is giving me the ...
1
vote
1
answer
39
views
SQLite inconsistency behavior with the NOT operator
As for example, I am using this website: https://sqliteonline.com/.
I try to fuzz around with queries and did as such:
SELECT * FROM demo
WHERE Name="SQL " || "Online"
Which ...
-1
votes
1
answer
41
views
How to overload decrement(--) operator for functions which consist of other structure/classes
I found a lot information how to overload decrement(--) operators if your class has an int value.
But I can not understand how to do the same with classes which contain structures which contain int ...
-1
votes
1
answer
32
views
Condition checking gives wrong answer
#include <stdio.h>
int main(){
printf("%d,%d\n", 2 & (1<<1) , 2 & (1<<1)>0 );
return 0;
}
the output of this program is 2,0.
2 & (1<<1) is equal ...
0
votes
0
answers
25
views
argument precedence for increment operator while printing output in c [duplicate]
So i was learning c and the tutor on this subject was like compiler takes arguments from right to left while printing in c. okay i understood that part but why does it print output 5 5 3 when i give a=...
2
votes
2
answers
67
views
Evaluation order of augmented operators (delimiters) in python
If I evaluate the following minimal example in python
a = [1, 2, 3]
a[-1] += a.pop()
I get
[1, 6]
So it seems that this is evaluated as
a[-1] = a[-1] + a.pop()
where each expression/operand would ...
0
votes
0
answers
21
views
What is the precedence between printf and ++x and x++? [duplicate]
When I execute this code:
#include <stdio.h>
int main(){
int x = 10;
printf("%d %d %d %d\n", ++x, ++x, ++x, ++x);
x = 10;
printf("%d %d %d %d\n", x++, x++, x++, x++);
x ...
7
votes
3
answers
83
views
Precedence inside a function call
Using the defined-or operator ( // ) in a function call produces the result I'd expect:
say( 'nan'.Int // 42); # OUTPUT: «42»
However, using the lower-precedence orelse operator instead throws an ...
0
votes
0
answers
10
views
Operator precedence - how far do operands go?
Consider this code snippet:
true || false && false
&& usually has higher order precedence than || which turns this expression basically into
true || (false && false)
What I ...
0
votes
1
answer
54
views
Morphism, product, coproduct operator precedence and associativity
I am writing a parser that will parse a simple functional toy language.
I'm having tough time with operator precedence and associativity of morphism, product and coproduct operators.
My toy language ...
1
vote
1
answer
134
views
In what order does CMake evaluate OR and AND in compound 'if' condition?
The CMake documentation states:
The following syntax applies to the condition argument of the if,
elseif and while() clauses.
Compound conditions are evaluated in the following order of
precedence: ...
3
votes
4
answers
215
views
Precedence of function application
In order to illustrate function application has the highest precedence in Haskell the following example was provided (by schoolofhaskell):
sq b = b * b
main = print $
-- show
sq 3+1
-- /show
...
0
votes
1
answer
69
views
Why does instanceof and addition get executed in the wrong order?
When I simply write the code
String x = null;
System.out.println(x instanceof Object);
The output is false, as expected.
But when I try to write it slightly different, namely as:
public class Main {
...
1
vote
0
answers
20
views
I have been trying to understand operator precedence but got stuck in this question
I have been trying to understand operator precedence but got stuck in this question.Here is the code:
int i=1;
int a= ++i + ++i + ++i * i++ + i++;
When I calculate using pen and paper, I am getting ...
0
votes
0
answers
149
views
How do I implement a parser that respects order of operations in a stack-based AST?
I have a parser that parses the following arithmetic
1 + 2 * 2
Into the following stack AST: Const(1) Const(2) Add Const(2) Mul.
I need it to parse into this stack AST Const(2) Const(2) Mul Const(1) ...
2
votes
1
answer
124
views
Why do bitwise 'and', 'xor' and 'or' have different precedences?
What is the reason behind giving &, ^ and | different precedence levels in C? I'm asking for C because most modern languages inherit similar precedence from C. But I don't know much about ...
0
votes
1
answer
196
views
How does a computer follow the BODMAS rule?
Given a simple arithmetic expression i.e.,
3+2*5-6
When calculated using python as the language
>>>3+2*5-6
7
As has been taught to us in our schools that in the given expression
We first ...