Asignment operators and miscelaneous¶

you can watch parallel the video for C/C++ (german): https://www.youtube.com/watch?v=2x1S19hzx_A&list=PLEWVM-KBUSpmWSfyoFdD_hLWAY_9tTgi5&index=16 https://www.youtube.com/watch?v=BcxHKvBlZwc&list=PLEWVM-KBUSpmWSfyoFdD_hLWAY_9tTgi5&index=17

In [1]:
import ROOT
In [2]:
import fortranmagic
%load_ext fortranmagic
import os
import sys

import numpy as np

if sys.platform.startswith("win"):
        # Depends of system, python builds, and compilers compatibility.
        # See below.
    f_config = "--fcompiler=gnu95 --compiler=mingw32"
else:
        # For Unix, compilers are usually more compatible.
    f_config = ""

    # Disable only deprecated NumPy API warning without disable any APIs.
f_config += " --extra '-DNPY_NO_DEPRECATED_API=0'"

%fortran_config {f_config}
New default arguments for %fortran:
	 --extra '-DNPY_NO_DEPRECATED_API=0'

Asignment operators¶

The asignment operators have right to left associativity for all four languages and lowest precedence.

C/C++¶

In [3]:
%%cpp
int a=1,b=1,c=1;
a+=b+=c;
cout <<a<<b<<c<<endl;
a=b=c=1;
(a+=(b+=c));
cout <<a<<b<<c<<endl;
321
321

Javascript¶

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

let a=1,b=1,c=1;
a+=b+=c;
console.log(a)
console.log(b)
console.log(c)
a=b=c=1;          //here ; is somehow needed otherwise javascript thinks it goes on in the next line
(a+=(b+=c))
console.log(a)
console.log(b)
console.log(c)

Python¶

The increment asignment operators cannot be chained in python.

In [5]:
a=b=c=1 #declaration and asignment can be chained in python
b+=c;
a+=b;
print(a,b,c)
3 2 1

In all 4 langanges the logic operators have lower precedence than the comparison operators and consequently the following expressions do no need brackets.

Fortran¶

In [6]:
%%fortran 

! program and subroutine exchanged due to jupyternotebook

! program main
subroutine main()
    implicit none

    integer :: a=1,b=1,c=1
    b=b+c
    a=a+c
    print *,a,b,c
! end program
end subroutine main
In [7]:
main()
           2           2           1

Conditional operator¶

In c/c++ and javascript is the ternary conditional operator. It has after the assignment operator the lowest precedence. And like assignment operator it is right to left associative.

C++/C¶

In [8]:
%%cpp

cout <<( -10+1>0 ? 5:6)<<endl;  //bracketing is important here
cout <<( -10+(1>0 ? 5:6))<<endl;
6
-5

Javascript¶

In [9]:
%%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(-10+1>0 ? 5:6)
console.log( -10+(1>0 ? 5:6))

Python¶

In [10]:
print( -10 + 5 if (1>0) else 6)
print( -10 + (5 if (1>0) else 6))
-5
-5

Fortran¶

it does not exist.