11. Program control: DO loops and IF statements A program normally executes statements in the order that they appear.
That is, the first statement is executed first, and then each subsequent
statement is executed in turn until the end is reached. Sometimes this will
be too simple for your programming needs. In this section of the course,
you will see how to repeat sections of your program with do loops and how
to use if statements to choose whether to execute parts of your program.
11.1 DO... END DO loops
A simple do loop has the form:
do index=istart,iend,incr
statement 1
statement 2
statement 3
etc
end do
This do loop is contained between two statements: the do statement and an end do statement. All the statements between these lines are executeda number of times. On the first pass through the loop, the variable index has the value istart. After this first pass, index is incremented by an amount
incr each time the program goes through the loop. When the value of index is greater than iend (or, if incr is negative, when index is less than iend) then the statements inside the loop are not executed and program execution continues at the line after the end do statement. If the increment incr is omitted then, by default, incr is assumed to be 1.
Note that istart, iend and incr may be positive or negative constants,
variables or expressions, but they should always be integers.
Notice that the statements in the example do loop are indented by a few
extra spaces. This is a good programming style to use. It helps you see
easily where a loop begins and ends.
Warnings!
• Never use any of the statements inside the loop to alter the do
statement variables index, istart, iend or incr.
• Never attempt to jump into the body of a do loop from outside the loop -
this is illegal.
• Do not assume that index will have the value iend after normal conclusion of the do loop. Assume that it is undefined. If exit occurs from the do loop before the normal conclusion, e.g. by use of an exit statement (see later), then index will retain its value.
Exercise
Type the following program into a file called roots1.f90 and then try running
it with trial values of n.
program roots1
implicit none
integer:: i,n
real:: rooti
!
! Program to demonstrate the use of a DO loop.
!
write(*,*)' Enter an integer'
read(*,*)n
do i=2, 2*n, 2
rooti=sqrt(real(i))
write(*,*) i, rooti
end do
stop
end program roots1
index i set to 2, then executes all statements down to the end do statement. In the first pass through the loop, the numbers 2 and √2 are
displayed on the screen. At the beginning of the second pass i is incremented by 2 and takes the value 4. As long as i is not greater than
2*n, the statements will be executed again and the numbers 4 and 2 will be
displayed on the screen. The loop will continue until the last numbers (2n
and √2n ) are displayed.
Exercise
Alter the program roots1.f90 so that it displays the results in reverse order.
Name the altered program roots2.f90.
Exercise
In part 3 of this course you wrote a program results1.f90, which recorded
the exam marks of 5 students. The head of department is so pleased with
your program that he would like a version that can handle large classes.
Unfortunately, your original output layout is not suitable for larger numbers
of students. Modify your program so that it uses a do loop to display the
arrays in a vertical layout rather than a horizontal one, e.g:
Hint: you may not know how many students are in a class. To make sure
that your arrays are big enough to handle a whole class, either use
allocatable arrays or declare the arrays with the maximum dimensions that
you think they could need.
11.2 IF statements
An if statement allows you to choose to execute a part of your program if a
certain condition is true. The example below shows the general layout,
which is similar to a do loop:
if (logical expression) then
statement 1
statement 2
statement 3
etc
end if
The if and end if statements enclose a block of statements, which shouldbe indented to help you see where the if block starts and ends. The if statement contains a logical expression in brackets, which can have a
result of either .true. or .false.. If the result of the logical expression is .true. then all the statements inside the block are executed. If the result of
the logical expression is .false. the statements are not executed.
The logical expression in an if statement is constructed from a special syntax which uses logical operators. These are similar to normal operators but do not set the values of variables - they just compare them. Some
examples of logical operators are given below, together with an illustration of their use:
Note that there are two forms of most of the logical operators. Use whichever you prefer. There are two forms because only the older form
(e.g. .le.) was available in previous versions of Fortran and this form is still permitted as an alternative to the newer form.
A logical expression is evaluated in the following order:
1) Arithmetic operations are evaluated first and then followed by, in order:
2) .eq., .ne., .gt., .ge., .lt. and .le. have equal precedence and are
evaluated from left to right. These operators are identical to: ==, /=, >, >=, < and <=.
3) .not. operators.
4) .and. operators, from left to right.
5) .or. operators, from left to right.
As with arithmetic operations, you can use brackets to change the order of
evaluation.
Warning! .eq. should NOT be used to compare the values of real or
double precision variables. Real variables are held only to machine precision so, instead of using .eq. to see if they are equal, use .ge. or .le. to
check whether the difference between two real numbers is small.
Logical variables need not be compared to anything, since they can only have the values .true. or .false. . If mylog is a logical variable, then it can
be used in a logical expression as follows:
if(mylog) . succeeds if mylog is .true.
if(.not.mylog) succeeds if mylog is .false.
A more complicated example of a logical expression is the following:
if (a.le.b.and.i.eq.n.and..not.mylog) then
statements
end if
which identical to:
if (a<=b.and.i==n.and..not.mylog) then
statements
end if
The block of statements inside the if statement will only be executed if the value of the variable a is less or equal to that of b and i is equal to n and
the logical variable mylog has the value .false. (i.e. if .not.mylog = .true.).
Note that two full stops are needed when two old-style logical operators are
next to each other (.and. and .not. in this case).
An if statement can be extended to choose between two options by including an else statement:
if (logical expression) then
first block of statements
else
second block of statements
end if
The choice can be extended even further by using one or more else if
statements, e.g:
if (logical expression 1) then
statement block 1
else if (logical expression 2) then
statement block 2
else
statement block 3
end if
Note: if there is only a single statement inside the if block, then a single line form of the if can be used, e.g:
if (logical expression) statement
The then and end if statements are not required in this case.
Exercise
To test the use of the if statement and logical expressions, try out the
following program, testif.f90:
program testif
implicit none
real:: a,b
logical:: logicv
!
! Simple program to demonstrate use of IF statement
!
write(*,*)' Enter a value for real number A '
read(*,*)a
write(*,*)' Enter a value for real number B '
read(*,*)b
write(*,*)' Enter .true. or .false. for value of logicv '
read(*,*)logicv
!
if (a<=b.and.logicv) then
write(*,*)'a is less or equal to b AND logicv is .true.'
else
write(*,*)'Either a is greater than b OR logicv is .false.'
end if
!
stop
end program testif
Exercise
Modify your program testif.f90 to include a do loop which runs through the
test in the above program for five different sets of variables before coming
to a stop. Don't forget to modify the indentation accordingly!
11.2.1 More about the where statement
Earlier in the course the where statement was introduced. This is similar to
an array version of the if statement and also uses logical expressions to
determine which actions to take. As for the if statement, there is a longer
form of the where statement that allows you to perform several array
operations under the same logical conditions. For example:
where(a>0)
b = log(a)
c = sqrt(a)
end where
and it is also possible to specify operations to perform when the condition is
not true:
where(a>0)
b = log(a)
c = sqrt(a)
elsewhere
b = -999.0
c = -sqrt(a)
end where





0 Comments