Operator¶

you can watch parallel the video for C/C++ (german): https://www.youtube.com/watch?v=5iL-onZ6M5U&list=PLEWVM-KBUSpmWSfyoFdD_hLWAY_9tTgi5&index=13

Be aware of differences in operator precedence between c/c++ javascript on the one side and python on the other side especially with regard to the bitwise operators and equality and comparison operators. https://stackoverflow.com/questions/8649738/cs-heritage-bitwise-operators-vs-equality-operators-precedence https://www.sololearn.com/en/Discuss/1823439/difference-of-operator-precedence-between-c-and-python

we will see that in detail in the next chapter about bitwise operators

In [16]:
import ROOT

C++/C and Javascript¶

https://discuss.codechef.com/t/operator-precedence-table/14545

C++¶

image.png

C¶

image.png

javascript¶

image.png

In [17]:
%%cpp

cout <<3+4*5<<endl; //Precedence
23
In [18]:
%%cpp
int c=1;
++c;
cout <<c<<endl;
2
In [19]:
%%cpp
int c=1;

cout <<++c<<endl;
2
In [20]:
%%cpp
int c=1;

cout <<c++<<endl;
cout <<c<<endl;
1
2
In [21]:
%%cpp
int c=1;

cout <<--c<<endl;
cout <<c--<<endl;
cout <<c<<endl;
0
0
-1
In [22]:
%%cpp
int c=1;
c-=1;
cout <<c<<endl;
c+=1;
cout <<c<<endl;
c*=2;
cout <<c<<endl;
c/=2;
cout <<c<<endl;
c=11;
c%=10;               //Modulo operation
cout <<c<<endl;
0
1
2
1
1

There is no power operator in c/c++. You need to use a function from cmath. Attention ^ is the binary xor operator.

In [23]:
%%cpp
#include <cmath>
cout <<pow(2,3);
8

Javascript has the power operator ** wich is equivalent to Math.pow(a,b) with the highest precedence right between 14 and 15 and is not mentioned in the above table.

In [24]:
%%js //the next line is only necessary in jupyter notebooks
element.setAttribute('style', 'white-space: pre;');console.log=function(text){element.textContent+=text+"\n"}

console.log(2**5)
console.log(2**(3**4))
console.log(2**3**4)

Python¶

Be aware of a sutle difference in the operator precedence of the Comparison operators as compared to c/c++ where the equality and unequality operator have less precedence. https://learningmonkey.in/courses/python/lessons/operator-precedence-and-associativity-in-python/ image.png

In [25]:
print(2**4) 
16

Remark¶

Python behaves different with regard to the precedence comparison operator has the same precedence to equality or inequality operator:

In [26]:
print(1<2==1)
#should according to table be evaluated as
print((1<2)==1) #oops but that is not the case
#the comparison operators are actually a special case as the table above is actually wrong here
#because python allows for 
print(1<2<3) 
#and is to be read as
print(1<2 and 2<3)
#lets check
print(1<2 and 2==1) #yep
False
True
True
True
False
In [27]:
%%cpp
cout << (1<2==1) <<endl;
//this is evaluated as
cout << ( (1<2)==1) <<endl;
1
1
In [28]:
%%js //the next line is only necessary in jupyter notebooks
element.setAttribute('style', 'white-space: pre;');console.log=function(text){element.textContent+=text+"\n"}

console.log((1<2==1))
In [29]:
2**3**4==2**(3**4) # the ** operator is Right to left associative
Out[29]:
True

Fortran¶

https://sceweb.sce.uhcl.edu/helm/WEBPAGE-Fortran/myfiles/fortran-operators.html image.png