FORTRAN Tutorial Lesson 4 : Math and relational operators

I suggest you to go through previous lessons before proceeding here.

Let me first tell you about a few other things before going to math and relational operator.

Warm UP

First thing to remember, Always declare the variable first before giving value to variable.

We can write two different statements in FORTRAN in a single line using';'

for e.g:

program name
    implicit none
    integer :: first_var, second_var
    first_var = 4
    second_var = 8
end program

Above code can be written as:

program name
    implicit none
    integer :: first_var, second_var
    first_var = 4;    second_var = 8
end program

The use of variable is must in any Programming Language. So Know about the variable in FORTRAN from here.

Math Operator

Now, let’s talk about math operators in FORTRAN.

FORTRAN supports basic math operation like addition, subtraction… and more just like other programming language.

The below code uses all math operators available in FORTRAN:

program name
    implicit none
    integer :: first_var, second_var
    first_var = 4;    second_var = 3
    print*, first_var + second_var   ! + is addition
    print*, first_var - second_var   ! - is subtraction
    print*, first_var * second_var    ! * is multiplication
    print*, first_var / second_var   ! / is division
    print*, first_var ** second_var   ! ** exponent (to the power)
end program

The output of above code is:

7
1
12
1
64

The fourth answer i.e (answer of division), might be confusing to understand, as 4 / 3 should give 1.3333 but the answer is 1.

Since we declared our first_var and second_var as a integer, the division of those integers will give integer value in FORTRAN. so we don’t get decimal values in integer division.

So let’s change the data type from integer to real, this time.

program name
    implicit none
    real :: first_var, second_var
    first_var = 4;    second_var = 3
    print*, first_var + second_var   ! + is addition
    print*, first_var - second_var   ! - is subtraction
    print*, first_var * second_var    ! * is multiplication
    print*, first_var / second_var   ! / is division
    print*, first_var ** second_var   ! ** exponent (to the power)
end program

The output becomes:

7.00000000
1.00000000
12.00000000
1.33333337
64.00000000

Now if we see the fourth answer, we get the correct answer in decimal place. The numbers in the decimal place might be incorrect as it depends upon the precision, which can be changed using KIND specifier, as already discussed in the previous lesson.

We declared real data type for the variable first_var and second_var but gave the integer value to them, Don’t get confused here.

real :: value = 4
! it is same as
real :: value = 4.0
! but 
integer :: value = 4.0     ! This is wrong.

Now let’s make our code short, instead of using print*, statement 5 times, lets print the output in single line.

program name
    implicit none
    real :: first_var=4, second_var=3
    print*, first_var + second_var, first_var - second_var, first_var * second_var, first_var / second_var, first_var ** second_var 
end program

Now the output in one line as:

7.00000000      1.00000000        12.00000000          1.33333337       64.00000000

Relational Operator

FORTRAN also supports relational operator, like, greater than, less than and more.

The relational operator gives the value in Logical operator only i.e either True or False so they are used only to check the condition, not to calculate the values like math relation is used.

The below code shows the use of all relational operators used in FORTRAN:

program name
    implicit none
    real :: first_var, second_var
    first_var = 4.0
    second_var = 3.0
    print*, first_var == second_var  ! == to check whether they are equal or not, Returns True if they are equal, otherwise False
    print*, first_var > second_var   ! > returns True if first_var is greater than second_var, otherwise False
    print*, first_var < second_var    ! returns True if first_var is less than second_var, otherwise False
    print*, first_var >= second_var   ! returns True if first_var is greater or equal to second_var, otherwise False
    print*, first_var <= second_var   ! returns True if first_var is less or equal to second_var, otherwise False
    print*, first_var /= second_var   ! returns True if first_var is not equal to second_var, otherwise False
end program

The output is:

F
T
T
F
F
T

Instead of a direct sign for the logical operator, we can write their short form like .gt. for >=, .eq. for == etc. The code below shows the relational operators in the short form:

program name
    implicit none
    real :: first_var, second_var
    first_var = 4.0
    second_var = 3.0
    print*, first_var .eq. second_var  ! .eq. means equal to
    print*, first_var .gt. second_var  ! .gt. means greater than
    print*, first_var .lt. second_var  ! .lt. means less than  
    print*, first_var .ge. second_var  ! .ge. means greater or equal to
    print*, first_var .le. second_var  ! .le. means less or equal to
    print*, first_var .ne. second_var  ! .ne. means not equal to
end program

Note To Remember

To check equality, we have to use two equal sign '==' and to assign value, we have to use one equal sign '='. See the code below:

....
integer :: a, b
a = 5     ! assigning value
b = 6     ! assigning value
print*, a == b   ! checking their equality, a relational operator
.....

Let’s revise Variable once again!

Lets go back to the variable uses once again.

integer :: number = 5
print*, "I have apples"   ! to print text, use "" or ''
print*, number        ! to print variable value, don't use quotes

Variable are used to hold certain value, so when we need that value, we simply write the variable name, but to print out the information, that we want to say, we need to surround those text (information) by quotes.

Let’s try to combine variable and text.

integer :: number = 5
print*, "I have", number, "apples"

Understand the print*, statement here. “I have” is the text we need to display, so it is surrounded by quotes, number in the middle is a variable name, so need to write as it is and the last part “apples” is also text, so surrounded by quotes. These three parts are separated using a comma ,

Note: Use comma to separate the text and variable

The output of above code is:

I have 5 apples

The same result can be obtained using assigning those text to variable.

program example
   implicit none
   integer :: number = 5
   character(len= 15) :: first_part, last_part
   first_part = "I have"
   last_part = "apples"
    print*, first_part, number, last_part
end program

In the print*, statement, all three parts are variables. So we just separate them by comma, we didn’t use any quotes.

The output is:

I have 5 apples.

Enjoy practicing code.

You can also view the full playlist of FORTRAN TUTORIAL from this link.

Watch video on math and relational operators for more understanding.

Share this post:

Leave a Comment

Your email address will not be published. Required fields are marked *