Sage-Code Laboratory
index<--

Program Workflow

Control flow statements are managing the logic workflow of the application. It makes the program non linear. Most languages are using control flow to resolve problems step by step. Fortran is the original, other languages are just inheriting these ideas and create diverse variations.

Page bookmarks

Use these bookmarks to navigate down to learn a particular statement. Use browser back button to jump back here and learn the statement in random order. If you are first time here, scroll down and read everything.



Conditional

Conditionals are Logic expressions that can return value .true. or .false. Conditional expressions are usually enclosed in round brackets when used in statements. Also, you can combine multiple conditional expressions in larger expressions using logic operators. Conditionals are used to create branches, ladders and conditional loops.

Example

if ((a .gt. b) .and. .not. (a .lt. c)) then ...    

Branches

This is a set of 2 alternative blocks of code. The first block is executed when the conditional is true. The second is executed when conditional is false. The second block is optional.

decision logic diagram

Decision Branch

syntax pattern
 if (logical_expression1) then
    ! True branch
    ...
 else
    ! False branch
    ...
 end if

Ladder

A decision ladder is a set of exclusive conditions. Each condition enable or disable execution of next block. When all conditions fail the default block that has no condition is executed. The default block is optional.

decision logic diagram

Decision Ladder

syntax pattern
 if (logical_expression_1) then
    ! First branch
 else if (logical_expression_2) then
    ! Second branch
 ...
 else if (logical_expression_N) then
    ! N'th branch
 else
    ! Default branch    
 end if

Repetition

Repetitive statements are also called loops. In Fortran there are several kind of loops controlled in different ways. You must always have at least one interruption in a loop, otherwise you can create by mistake an in infinite loop that can jam your computer!

keywords

do - until

Fortran 95 has a consistent syntax. You can use keyword do for all loops in differed ways. The most simple loop is actually unconditional, infinite loop that should be avoided in practice. To make a "until" stop condition you need to use conditional "if" statement with keyword "exit".

do loop diagram

do-until loop

example
! do-until loop
program until
  integer:: i = 0
  do  
    print *, i
    i = i + 1
    if (i > 4) then 
       exit
    end if
  end do
end program

do - while

This kind of loop execute a block of code as long as condition is true. It do not start if the condition is false in the first place. It can be interrupted by an exit statement using a secondary condition.

while loop diagram

do-while loop

example
! conditional loop
program loop
  integer:: i = 0
  do while (i<5)
  print *, i
    i = i + 1
  end do
end program

range - loop

Range loop is used to iterate over a limited number of values. It is useful to iterate over array elements or matrix or do other things that can depend on an index number. It can progress ascending or descending with a specified positive or negative ratio.

example
! range scan
program for

do i=1,10,2 !ratio +2
  print *, i
end do

print *,""
do i=10,1,-2 !ratio -2
  print *, i
end do

end program
console
> ./for
           1
           3
           5
           7
           9
 
          10
           8
           6
           4
           2
>

Selection

Selection statement is similar to switch statement of other languages. This is basically a jump table controlled by a single variable. It can have multiple cases and one default case. This statement can be used to create menu option in a CLI app or do other things like in example below.

selection diagram

Selection Diagram

example
! number of days in a month
program main
integer:: month, num_days
write (*,*) "month #"
read *, month
select case (month)
    case (2)
        num_days = 28
    case (7:8)
        num_days = 31
    case (1,3,5,10,12)
        num_days = 31
    case default
        num_days = 30
end select
print *, "num days = ", num_days
end program
console
>./select
 month #
2
 num days =           28
>./select
 month #
1
 num days =           31
>./select
 month #
3
 num days =           31
>./select
 month #
9
 num days =           30
>    

Notes: Cases are exclusive and the default case is optional. You can have a list of cases separated by coma and a range separated by column. Cases should not overlap. Only one case block is execute. You do not have to use break like in other languages.

Labels

In Fortran 77 we have a legacy system of numeric labels that is deprecated. Modern Fortran has preserved labels only to give names for specific block statements. The syntax is label:, if you try to use numeric labels, your code will not compile.

Deleted features:Related to labels, the ASSIGN statement was deprecated, and GO TO was replaced by "goto" statement. Though this statement is not recommended. You should use "cycle" and "exit" with a label if you use nested loop.

example
! using labels
program label
real :: r
integer i, j
i = 0 ! initialize the index
outer: do i=0, 3
  inner: do j=0, 6
    call random_number(r)
    if (modulo(j,2) == 0) then
       cycle inner;  
    end if;
    print "(i2,i2,f8.2)",i, j, r
  end do inner 
end do outer
print *, "done"
end program
console
> ./label
 0 1    0.39
 0 3    0.24
 0 5    0.29
 1 1    0.26
 1 3    0.30
 1 5    0.90
 2 1    0.01
 2 3    0.52
 2 5    0.23
 3 1    0.29
 3 3    0.55
 3 5    0.05
 done

Read next: File Handling