0% found this document useful (0 votes)
3K views4 pages

Fortran Basics

Uploaded by

mojex34212
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3K views4 pages

Fortran Basics

Uploaded by

mojex34212
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

! Test1_PVB_20211222.

f90
!
! FUNCTIONS:
! Test1_PVB_20211222 - Entry point of console application.
!

!****************************************************************************
!
! PROGRAM: Test1_PVB_20211222
!
! PURPOSE: Entry point for the console application.
!
!****************************************************************************

program Test1_PVB_20211222

implicit none

! Variables
! 1.1 Variable Declaration

integer, parameter :: rk = selected_real_kind(15,300)

! a and A are same


integer :: a, b ! Scalar integer
real :: c, d ! scaler floating pt
logical :: flag ! boolean

real :: e(10) ! floating ot array of 10 elements


real, dimension(10) :: ee ! integer arraye of 10 elements

real :: f(20,20) ! -"- 20x20

character :: ch ! single character


character, dimension(10) :: chv ! arraye of 10 characters
character(len=80) :: line ! character string
character(len=80) :: lines(60) ! character string

integer, parameter :: C1 = 5 ! integer constant


real :: C_array(C1) ! Array of C1 elements

! 1.2 Variable precision


! by default takes the smellest precision, e.g. real - 4 bite precision
! special directives can be used to specify precision independent of architecture

! A. Explicit precision specifiers:


real(8) :: g ! 8 bite floating point = double in C++ (on intel platform)
real(4) :: h ! 4 bite floating point = float in C++ (on intel platform)
integer(4) :: I !

! B. Using selected_real_kind(15, 300)


integer, parameter :: ap = selected_real_kind(15,300) ! 15 = Significant decimal, 300 = power
i.e. 10^-300 to 10^300
real(kind = ap) :: pi1, pi2 ! OR real(ap)

!pi1 = 3.141592653589793 ! Doesn't give correct value


!pi2 = 3.141592653589793_ap ! gives correct value
!write(*,*) 'pi1 = ', pi1
! write(*,*) 'pi2 = ', pi2

! 1.3 General type rule


! variables starting with I...N are interger
! Other are real
! implicit none = to avoid this

! 1.4 Variable assignment


! pi2 = 3.141592653589793_ap
!flag = .false. ! OR .true.
! ch = 'Jan' ! OR "Jan"
! "Don't" OR 'Don"t' = Don't

! 1.5 Defined vs Undefined variables


! A variable is Undefined until it has been assigned a value
! Undefined variable must not be referred

! 2.1 Arithmaetic Operators


! +-/*, ** = popwer

! 2.2 Relational operator


! < = .lt. , > = .gt. , <= .le' == = .eq. , /= = .ne.

! 2.3 Logical operators


! .and. .or. .not.

! 3.1 Integer expressions


! 6/3 = 2, 8/3 = 2, -8/3 = -2
! 2**3 = 8, 2**(-3) = 1/2**3 = 0

! 3.2 Mixed datatypes


! Weaker ones will be convertede to stronger ones, i.e. integer to real

!**********************************************
! 4.1 Array expressions
! real, dimension(10, 20) :: aa, bb
! real, dimension(5) :: v
! a/b ! Array(10,20) with elements aa(i,j)/bb(i,j)
! v+1. !Array(5) elements v(i)+1
! 5/v+a(1:5,5) ! Array(5) elements 5/v(i) + a(i,5)
! a.eq.b !true if all elements are equal

! real(rk) :: aaa(10,20), bbb(10,20), ex1(10,20)


! real(rk) :: vvv(5), ex2(5), ex3(5)
! integer :: ii, ccc
! logical :: ex4(10,20)

! aaa = 20.0_rk ! all elements 20.0


!aaa(1,1) = 2.0_rk ! aa(1,1) = 2.0
!bbb = 2.0_rk
!vvv = 2.0_rk

! ex1 = aaa/bbb
! ex2 = vvv + 2.0_rk
!ex3 = 5/vvv + aaa(1:5,5)
! ex4 = aaa.eq.bbb
!**********************************************

! Arrays and matices


! default start is 1 NOT 0
! can be redefined, e.g
! real(rk) :: idx(-3,3) ! gives indices = [-3,-2,-1,0,1,2,3]
! v = (/ 1.2, 2.1, 3.5, 4.5, 5.6/) ! multiple values in sigle assignment

! Assigning slices
! real :: A(4,4)
! real :: B(4)
! real :: C(4)
! B = A(2,:)
! C = A(:, 1)

! K(5,:) = (/ 5.0, 4.0, 3.2, 2.5, 1.2/)


! Arrays are stored columwise

!*******************************
! Conditionals

!************* if *************
! if (expr) statement

! if (expr) then
! block
! end if

! if (expr) then
! block 1
! else if (expr) then
! block 2
! else
! block 3
! end if

! ************* select *************


! select case (expr)
! case selector
! block
! end select

! select case (expr)


! case selector
! block
! case default
! block
! end select

!integer :: value
!write(*,*) 'Enter a Value'
!read(*,*) value
!
!select case (value)
!case (:0)
! write(*,*) 'Number less than 1'
!case (1)
! write(*,*) 'Number is 1'
!case (2:9)
! write(*,*) 'between 2 and 9'
!case (10:)
! write(*,*) 'greater than 9'
!case default
! write(*,*) 'This should not happen'
!end select

!************ do *************
!do i = 1,10
! j = j+1
!end do
!
!do i=2,30,4 ! 2,6,10,14,18,22,26,30
! j=j+1
!end do

!loop variable i is not allowed to chnage inside do statement


!integer :: ii,jj
!jj=0
!
!do ii = 1,5
! jj=jj+1
! print*,jj
!end do

!************ do while *************


! do while(expr)
! block
! end do

!do while(jj<5)
! jj=jj+1
! print*,jj
!end do

!************ Matrix functions *************


! dot_product(u,v)
! matmul(A,B)
! tanspose(C)

! Body of Test1_PVB_20211222
print *, 'Hello World'

end program Test1_PVB_20211222

You might also like