!********************************************************************
! The example introduces some basic Fortran features, such as
! 1) Variable declaration
! 2) Printing on the screen
! 3) Printing in a user defined file
!********************************************************************
!********************************************************************
! Author: Giuseppe Forte
! First release: 25/05/2017
! e-mail: giuseppe.forte@giuseppeforte.me
! Website: https://www.giuseppeforte.me
! License: Creative Commons Attribution-ShareAlike 4.0
! International (CC BY-SA 4.0)
! (https://creativecommons.org/licenses/by-sa/4.0/)
!********************************************************************
program FundamentalsForDummies
implicit none
! DECLARATION SECTION. HERE WE DECLARE ALL
! THE VARIABLES
real*8 Radius,CircArea ! real variables in double precision
real*8 GreekPi ! if you want to declare variables in
!single precision, use the instruction "real" rather than
!"real*8".Remember that if you are interested in scientific
!computing, it is a good practice to declare always real
!numbers in double precision
integer YesNo ! integer variable
! let us print some information on the screen
print*,"**************************************************"
print*,"Hello guys, I print everything in between the "
print*,"quotation marks (refere to this source code) "
print*,"You can introduce your Fortran instructions using "
print*,"columns 10 to 71. After column 71 you may start a "
print*,"new line or you may go on writing your instructions
& on a new line, provided that you add the symbol '&' on
& column 6. (see this source code)"
print*,"**************************************************"
print*,""
print*,""
print*,"Now we may compute the area of a circle."
! First we calculate the value of GreekPi
GreekPi = 4.0d0*atan(1.0d0) !remember that the
!tangent of Pi/4 is 1, thus by inverting, we get the formula
!above (see any textbook on Calculus)
!we now read from the keyboard the value of the radius of
!the circle
100 print*,"Introduce the value of the radius:"
read*, Radius !read the value from the keyboard
CircArea = GreekPi * Radius * RAdiUS ! FOrTRAn is noT
! KEYSEnsItive, so Radius = RAdiUS
print*,"The area of the circle with Radius", real(RadiUS)
print*, "is given by:"
print*,real(CircArea)!try to print the result without
! the function "real(...)". Just write CircArea.
!Now let's try to write the result on an external file
write(*,*)"Now let's try to write the result on an external"
write(*,*) "file" ! as you can see,if we want to write
!something on the screen, we can use both the instruction
!"print*," , as before, or "write(*,*), as in the last two
!instructions"
!we need to open an external file and write inside it the
!value of the area. Here is the instruction to open the file
open(unit=27, file="CircleArea.dat", status="unknown")
! the number "27" is a label (your own choice) associated to
! the file name "CircleArea.dat" (your own choice). When you
! open a non existing file, always use the instruction
! status="unknown".
! We will see that when a file already exists, we may use the
! statement status="old" and other types of instructions. For
! now, we do not go inside these instructions...and now let's
! try to write something inside such file
write(27,*)"The area of the circle with Radius",real(Radius)
write(27,*)"is given by:"
write(27,*)real(CircArea)
!observe that now we use the instruction write(27,*).
!if we no longer need the file "CircleArea.dat", let us
!close it
close(27)
!if we open another file now, we could use the label 27
!again (AFTER the "close" instruction)
! a simple conditional statement
print*,"Do you want to compute the result again?"
print*,"type 1 for Yes and 0 for No"
read*, YesNo
if(YesNo == 1) goto 100 !100 is a label (your own choice)
!you are required to write the label only within the first
!three columns (look for the label 100 above, row 52).
! The instruction says "if YesNo = 1" (observe
!that in order to tell to the compiler the logical equivalence
!we write == and not =) jump to label 100. Try also to change
!this instruction with the following
!if(YesNo==1)then
! goto 100
!end if
! you'll see that the result is the same
print*,"Thank you for having fun with me. Cheers :-)"
print*,"END OF THE PROGRAM !!!!!!!!!!!!!!!!!!!!!!!"
end program FundamentalsForDummies