Date Modified Category Fortran Tags Fortran

Relational Operators

Fortran provides 3 types of operators:

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
# Operator Equivalent
1 == .eq.
2 /= .ne.
3 > .gt.
4 < .lt.
5 >= .ge.
6 <= .le.

Fortran Decisions

Fortran has 2 types of decision making statements. We will focus on if statement.

select case

select case (color)
   case ('green') 
     print*, "Pass!" 
   case ('yellow')
     print*, "Get Ready!"
   case ('red') 
      print*, "Wait!" 
   case default
      print*, "Invalid Color"
end select

if statement

if can be constructed like in the example below. It can nest/be nested within other statements.

if (count == 1) then
  print*, 'First element'
else if (count == 10) then
  print*, 'Last element'
end if

Fortran Loops

do loop

do i = 1, 10
  print*, 'Count', i
end do 

Loop Control Statements

Fortran do can be interrupted with exit and cycle commands. exit will terminate current loop, where as cycle will skip the rest of the current index, end do will determine if loop is completed or not.

stop also can be use to stop the loop as well as program itself.

do i = 2, 6
  do j = -2, 8
    if (j.EQ.5) exit
    do k = 1, 5
      if (k.EQ.3) cycle
      print *, 'I=', i, ' J=', j, ' K=', k
    end do
  end do
end do

One can also have loops with names.

iloop : do i = 2, 6
  jloop : do j = -2, 8
    if (j.EQ.5) exit
    kloop :  do k = 1, 5
      if (k.EQ.3) cycle
      print *, 'I=', i, ' J=', j, ' K=', k
    end do kloop
  end do jloop
end do iloop

while loop

Small example to show how to use Fortran's do while statement.

nSum = 0
i = 0
do while (i <= 10)       
   nSum = nSum + n 
   i = i + 1
   print*,  n, i
end do 

Exceptional Situations

exit : To exit the loop

cycle : Program jumps to the next iteration

stop : Execution of the program will stop

go to : Please see the examples and discussion by Arlene Guest.