Go through all previous lessons, before proceeding here.
Comment, range and precision are the tools to understand the data and its uses in FORTRAN programming language. Let’s go one by one.
What is comment?
Comment is an important part of any programming language. In any programming Language, all the codes are executed except comment. In other words, the text which are ignored by the compiler are comments.
It is an important tool to any programmer to increase their productivity. It is not mandatory to write. But, using it effectively, help to understand the code even better.
Programmer use comment for different proposes. — some use it to explain part of code, some use to leave a guide or mark to the code and many other uses. Different program uses different syntax to write comment.
How to write comment in FORTRAN?
In FORTRAN, any text after ‘
! ‘ is a comment.
integer :: my_age = 24 ! This is a comment
You can write as many comments as you want. You can write comments even in the code line too.
integer :: new = 45.67 ! new is the new variable
Range of data
Let’s talk about the range of data types:
First, you should know about the variables and data types in FORTRAN.
Let’s create a integer variable named cost for now:
integer :: cost
Now store value in that
cost variable. But question comes, how greater value of integer can we store in the
By default, we can store maximum up to 2^31 -1 number. So if we try to store number greater than that, we will get error.
This is called range and this range can be changed using one intrinsic function called
KIND is not specified, default value of that
KIND is 4. But we can change value to 8, and 16.
integer :: cost = 2147483648 ! error because that ! number is out of range, 2147483648 > 2^31 - 1 integer (kind=8) :: cost = 2147483648 ! correct because this ! variable has kind 8 so it can store up to 2^63 -1 number
So we understood the range of integer data types but what about the real data type?
Precision of data
Real data type includes numbers in decimal value. So, there we talk about the precision of decimal values. By default, we get the real values with 8 decimal place, but we can increase or decrease its decimal place using the same
real :: pi = 22.0/7.0 print*, pi ! output is 3.14285707, ( 8 decimal place)
now lets increase the precision of the value:
real (kind = 8) :: pi = 22.0 / 7.0 print*, pi ! output is 3.142857074375488
Hence, the precision is increased using
Variables are created to store the value. Sometimes, we have to create a variable which value remains fixed throughout the program or the value keeps changing.
If the value to be stored in unchangeable, that means constant value, then using parameter keyword makes the variable value unchangeable.
real, parameter :: G = 6.67 * 10e-11 ! parameter makes this value ! unchangeable
But if we need to change the value then we can reassign different value to the same variable.
logical :: ans ! ans variable is created with logical datatype ans = True ! True value is assigned. ans = False ! False value is reassigned
We can update the variable using the expression also.
integer :: value = 4 value = value + 6 ! updating the variable print*, value
Guess the output:
The second line code seems mistake, as we used value name on both sides, but its okay for any programming language.
First value is assigned 4, then
value = value + 6 means
value = 4 + 6, the first assigned value comes to the expression on right side of the equation and the calculation is done and new value is assigned to
The output of above code is
For more understanding, watch video Information on comments, range and precision.