Skip to main content Skip to navigation

5. Conditionals and Loops

1. if / else /endif

These conditional structures allow you to control the flow of your code. The if statement allows you to control if a program enters a section of code or not, based on whether a given condition is true or false. There are three basic variations on if, with general syntax given below:

if ( condition is true ) Execute this statement

or you could use a statement block

if ( condition is true ) then
  Execute this statement
  Execute this statement too
  etc..
endif

Both of these structures will only execute the statement(s) that follow if the statement is true. If you want to execute other code in the situation when the statement is false, you can use the if/else if/ else construct:

if ( condition1 is true ) then
   Execute these statements if TRUE
else if ( condition2 is true ) then
   Execute these statements if TRUE
else
   Execute these statements if neither condition is TRUE
endif

Putting it into Practice:

Code up the statements below to see how this kind of construct works.
program ageist
implicit none
integer :: age

print*,"please enter your age"
read*,age

if (age.lt.50) then
print*,"You are pretty young!"
else if (age==50) then
print*,"You are old!"
else
print*,"You are REALLY old!"
endif

end program ageist
Compile and run this code. Try writing some variations on this code.
Back to top

2. case statements

The case statement is another way to use decision constructs in f90.

Example:

Example of case statements below:
program numbers
implicit none
integer :: i

print*,"please enter a number"
read*,i

select case (i)
case (:-1) ! i is negative
print*,"number is negative"
case(0) ! only zero
print*,"number is zero"
case(1:9) ! i is positive
print*,"number is positive and less than 10"
case default
print*,"number is positive and greater than 9"
end select

end program numbers
You are not restricted to using integers - you could also select for characters. The default case (covers all other options) is optional.
Back to top

3. do loops

This construct is useful for running a particular statement or block of statements many many times. There are two clear cases - one when you don't have a pre-defined idea of how many times you need to execute your statement blocks in a loop and one where you do know how many times you'd like your loop to run. Dealing with the first case, you can use a conditional statement to test for a loop exit - an example of usage is given below:

program loopy
implicit none
integer :: i

i = 0

do
i = i+1
print*,i
if (i>99) exit
end do

end program loopy

Notice that you can put the conditional/exit statement anywhere in the body of the loop - that way you can control how much of the loop is executed before the exit occurs. Putting the conditional at the end of the loop ensures the statements will be executed at least once.

A variation on this construct is to use the cycle statement instead of the exit statement. This loop construct ensures that when the conditional returns true, the loop the loop will skip over that value of the loop counter, eg

program loopy
implicit none
integer :: i

i = 0

do
i = i+1
if (i.gt.100) exit
if (i==80) cycle
print*,i
end do

end program loopy

Try coding this up and running it - the output will be a printout of all numbers from 1 to 100 except 80.

The second instance of do loops is for the case when you know how many times you wish to cycle - otherwise known as an indexed loop. The syntax of the indexed loop is:

do variable = start, finish, stride

The "stride" (interval between steps) is optional - the default is an increment of positive 1. Note that the stride can be a positive or negative number.

Example:

Example of an indexed loop below:
program loopy
implicit none
integer :: i

i = 0

print*,"forwards"
do i=1, 5
print*,i
end do

print*,"backwards"
do i = 5, 1, -1
print*,i
end do

end program loopy
You are not restricted to single loops - you can also nest loops.
Back to top