Conditions and Loops¶
you can watch parallel the video for C/C++ (german): https://www.youtube.com/watch?v=aob0SwTaF2Q&list=PLEWVM-KBUSpmWSfyoFdD_hLWAY_9tTgi5&index=17 https://www.youtube.com/watch?v=gjRcvn9TM-o&list=PLEWVM-KBUSpmWSfyoFdD_hLWAY_9tTgi5&index=18 https://www.youtube.com/watch?v=lxNX3jXdo6M&list=PLEWVM-KBUSpmWSfyoFdD_hLWAY_9tTgi5&index=19 https://www.youtube.com/watch?v=lCQii8m_5Fw&list=PLEWVM-KBUSpmWSfyoFdD_hLWAY_9tTgi5&index=20 https://www.youtube.com/watch?v=5bm9GxAr_4E&list=PLEWVM-KBUSpmWSfyoFdD_hLWAY_9tTgi5&index=21 https://www.youtube.com/watch?v=5cE_HfsLpP0&list=PLEWVM-KBUSpmWSfyoFdD_hLWAY_9tTgi5&index=22
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'
Conditions¶
c++¶
In [3]:
%%cpp
int i=7;
if(i==5){
cout<<"equals five!"<<endl;
} else if(i==6){ //this part can also be left out
cout<<"equals six!"<<endl;
} else { //this part can also be left out
cout<<"neither five nor six!"<<endl;
}
neither five nor six!
In [4]:
%%cpp
int i=5;
if(i==5) cout<<"equals five!"<<endl;
else if(i==6) cout<<"equals six!"<<endl;
else cout<<"neither five nor six!"<<endl;
equals five!
c¶
In [5]:
%%cpp
int i=5;
if(i==5){
printf("equals five!");
} else if(i==6){ //this part can also be left out
printf("equals six!");
} else { //this part can also be left out
printf("neither five nor six!");
}
equals five!
javascript¶
In [6]:
%%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 i=5
if(i==5){
console.log("equals five!");
} else if(i==6){ //this part can also be left out
console.log("equals six!");
} else { //this part can also be left out
console.log("neither five nor six!");
}
Python¶
In [7]:
i=5
if i==5:
print("equals five!")
elif i==6:
print("equals six!")
else:
print("neither five nor six")
equals five!
Fortran¶
In [8]:
%%fortran
! program and subroutine exchanged due to jupyternotebook
! program main
subroutine main()
implicit none
integer :: i = 5
if (i .eq. 5) then
print *,"equals five!"
else if (i .eq. 6) then
print *,"equals six!"
else
print *,"neither five nor six"
endif !"end if" is also possible
! end program
end subroutine main
In [9]:
main()
equals five!
Loops¶
c++¶
In [10]:
%%cpp
string s{"Hallo"};
cout<<"***** While Loop *****"<<endl;
int i=0;
while(i<10){//the same in c
cout <<i<<endl;
++i;
}
cout<<"***** Do-While Loop *****"<<endl;
do{//the same in c
cout <<i<<endl;
--i;
}while(i>0);
cout<<"***** For Loop *****"<<endl;
for(int i=0;i<10;++i){//the same in c
if(i==5) continue;
cout <<i<<endl;
if(i==8) break;
}
cout<<"This only works in c++"<<endl;
for(auto &c:s){
cout <<c<<endl;
}
***** While Loop ***** 0 1 2 3 4 5 6 7 8 9 ***** Do-While Loop ***** 10 9 8 7 6 5 4 3 2 1 ***** For Loop ***** 0 1 2 3 4 6 7 8 This only works in c++ H a l l o
C¶
In [11]:
%%cpp
char s[]="Hallo";
printf("***** While Loop *****\n");
int i=0;
while(i<10){
printf("%d\n",i);
++i;
}
printf("***** Do-While Loop *****\n");
do{
printf("%d\n",i);
--i;
}while(i>0);
cout<<"***** For Loop *****"<<endl;
for(int i=0;i<10;++i){
if(i==5) continue;
printf("%d\n",i);
if(i==8) break;
}
printf("***** Looping over a string in c *****\n");
//a c string is actually an array
for(int i=0;i<sizeof(s)/sizeof(char);++i){
printf("%c\n",s[i]);
}
***** While Loop ***** 0 1 2 3 4 5 6 7 8 9 ***** Do-While Loop ***** 10 9 8 7 6 5 4 3 2 1 ***** For Loop ***** 0 1 2 3 4 6 7 8 ***** Looping over a string in c ***** H a l l o
Javascript¶
In [12]:
%%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 s="Hallo"
console.log("***** While Loop *****");
let i=0;
while(i<10){//the same in c
console.log(i);
++i;
}
console.log("***** Do-While Loop *****");
do{
console.log(i);
--i;
}while(i>0);
console.log("***** For Loop *****");
for(let i=0;i<10;++i){
if(i==5) continue;
console.log(i);
if(i==8) break;
}
console.log("***** iterate over string c-style *****")
for (let i = 0; i < s.length; i++) {
console.log(s[i]);
}
console.log("***** iterate over string c++-style *****")
for (const c of s) {
console.log(c);
};
console.log("***** functional way but slower *****")
s.split('').forEach(function(c) {
console.log(c);
});
Python¶
In [13]:
s = "Hallo"
a=[1,2,3,4,5];
i=0;
print("***** while loop *****")
while i<10:
print(i);
i+=1
print("***** do while loop does not exist *****")
print("***** For loop *****");
for i in range(0,10):
if i==5: continue
print(i)
if i==8: break
print("***** iterate over string *****")
for c in s:
print(c)
print("***** iterate over array *****")
for i in a:
print(i)
print("***** iterate over array with index *****")
for i in range(len(a)):
print(a[i])
***** while loop ***** 0 1 2 3 4 5 6 7 8 9 ***** do while loop does not exist ***** ***** For loop ***** 0 1 2 3 4 6 7 8 ***** iterate over string ***** H a l l o ***** iterate over array ***** 1 2 3 4 5 ***** iterate over array with index ***** 1 2 3 4 5
In [14]:
for i in range(0,10):
print(i)
else:
print ("fertig")
0 1 2 3 4 5 6 7 8 9 fertig
Fortan¶
In [15]:
%%fortran
! program and subroutine exchanged due to jupyternotebook
! program main
subroutine main()
implicit none
character (len=5) :: s="Hallo"
integer :: i;
print *,"***** While Loop *****"
i=0;
do while(i .lt. 10)
print *,i
i=i+1
enddo ! "end do" also possible
do while(.true.)
print *,i
i=i-1;
if (i.le.0) exit
enddo
do i=0,10,1
if (i.eq.5) then
cycle
endif
print *,i
if (i.eq.8) then
exit
endif
enddo
do i=1,len_trim(s),1
print *,s(i:i)
enddo
! end program
end subroutine main
In [16]:
main()
***** While Loop *****
0
1
2
3
4
5
6
7
8
9
10
9
8
7
6
5
4
3
2
1
0
1
2
3
4
6
7
8
H
a
l
l
o
Switch Case¶
c++¶
In [17]:
%%cpp
for(int i=0;i<10;++i){
switch(i){
case 1:
cout<<"one"<<endl;
break;
case 5:
cout<<"five"<<endl;
break;
default:
cout<<"..."<<endl;
}
}
... one ... ... ... five ... ... ... ...
c¶
In [18]:
%%cpp
for(int i=0;i<10;++i){
switch(i){
case 1:
printf("one\n");
break;
case 5:
printf("five\n");
break;
default:
printf("...\n");
}
}
... one ... ... ... five ... ... ... ...
javascript¶
In [19]:
%%js //the next line is only necessary in jupyter notebooks
element.setAttribute('style', 'white-space: pre;');console.log=function(text){element.textContent+=text+"\n"}
for(let i=0;i<10;++i){
switch(i){
case 1:
console.log("one\n");
break;
case 5:
console.log("five\n");
break;
default:
console.log("...\n");
}
}
Python¶
supports match case since python Version 3.10
In [20]:
for i in range(0,10):
match i:
case 1:
print("one");
case 5:
print("five");
case _:
print("...");
... one ... ... ... five ... ... ... ...
Fortran¶
In [21]:
%%fortran
! program and subroutine exchanged due to jupyternotebook
! program main
subroutine main()
implicit none
integer :: i
do i=0,9,1
select case (i)
case(1)
print *,"one"
case(5)
print *,"five"
case default
print *,"..."
end select
enddo
! end program
end subroutine main
In [22]:
main()
... one ... ... ... five ... ... ... ...