You are on page 1of 6

Fortran Selection Statements

When writing Fortran program, it may be necessary to take some action based on the outcome
of comparing the values of some variables. All programming languages have some facility for
decision-making. That is, doing one thing if some condition is true and (optionally) doing
something else if it is not.

Fortran IF statements and/or CASE statements are used to allow a program to make
decisions.
Conditional Expressions
Relational Operators
The basic relational operators are:

For this course (GS543), the normal form will be used for examples in this text.

Note: A relational operation is used to form a conditional expression. The result of a conditional
expression must always result in either a true or false result. The “==” (two equal signs) is used
to compare. The “=” (single equal) is used for assignment (setting a variable). The “==” does
not change any values, while the “=” does change the value.
For example, given the declaration of,
real :: marks

it might be useful to know if the current value of marks is greater than certain value.
In this case, the conditional expression would be,
(marks > certain value)
Which will result in a true or false result based on the value of the variable marks.

Logical Operators

Logical operators are used between two logical variables or two conditional expressions.
Logical operators are used to combine conditional expressions as needed to form a more
complex conditional expression. For example, given the declaration of,
real :: marks

it might be useful to know if the current value of marks will help to assign the grade of
particular student. For example, the relational operator would be AND with the complete
conditional expression,

( (marks => 80) .and. (marks =<90) )

If result in a true, then we can assign the student grade ‘AB’ and if it is false result then we can
assign other grade. Since the AND logical operation is used, the final result will be true only
if both conditional expressions are true.

Another way of check the status to determine if the game should continue might be,

( (marks => 80) .or. (marks =<90) )

which still results in a true or false result. However, since the OR logical operation is used, the
final result will be true if either conditional expressions is true.

The relational operators (e.g., <, <=, >, >=, ==, /=) have higher precedence than logical
operators (AND, OR, NOT). This means each of the smaller conditional expressions will be
completed before the logical operation is applied.

A conditional expression can be a combination of multiple conditional expressions combined


with logical operators.

IF Statements

IF statements are used to perform different computations or actions based on the result of a
conditional expression (which evaluates to true or false). There are a series of different forms
of the basic IF statement. Each of the forms is explained in the following sections.

• IF THEN Statement
The IF statement, using the conditional expression, is how programs make
decisions. The general format for an IF statement can be demonstrated via following
example:
For example, given the declaration of,
real :: marks
based on the current value of marks, an appropriate IF statement might be;
if ( marks > 90 ) then
write (*, *) "grade is AA."
end if

which will display the message “grade is AA.” on the next line if the value of marks is greater
90.
Additionally, another form of the IF statement includes

if ( <conditional expression> ) <fortran statement>

In this form, only a single statement is executed if the conditional expression evaluates to
true. The previous example might be written as;

if ( marks > 0 ) write (*, *) " grade is AA."

In this form, “then” or “end if” are not required. However, only one statement can be executed.

• IF THEN ELSE Statement

The IF THEN ELSE statement expands the basic IF statement to also allow a series of
statements to be performed if the conditional expression evaluates to false.
The general format for an IF THEN ELSE statement is as follows:

if ( <conditional expression> ) then


<fortran statement (s) >
else
<fortran statement (s)>
end if

Where the <fortran statements> may include one or more valid Fortran statements.

Example:

For example, given the declaration of,


real :: marks
based on the current value of marks is, a reasonable IF THEN ELSE statement might be:

if ( marks => 90 ) then


write (*, *) "Grade is AA"
else
write (*, *) "Grade is other than AA"
end if

Which will display the message “Grade is AA” if the value of marks is greater than or equal
to 90 and display the message “Grade is other than AA.” if the value of marks is less than 90.

• IF THEN ELSE IF Statement


The IF THEN ELSE IF statement expands the basic IF statement to also allow a series of IF
statements to be performed in a series.

The general format for an IF THEN ELSE IF statement is as follows:

if ( <conditional expression> ) then


<fortran statement (s)>
else if (<conditional expression>) then
<fortran statement (s)>
else
<fortran statement (s)>
end if

or

if ( <conditional expression> ) then


<fortran statement (s)>
else if (<conditional expression>) then
<fortran statement (s)>
else if (<conditional expression>) then
<fortran statement (s)>
else if (<conditional expression>) then
<fortran statement (s)>
:
:
:
else
<fortran statement (s)>
end if

Example:

For example, given the declaration of,


real :: marks

based on the current value of marks, a reasonable IF THEN ELSE IF can be written as

if ( marks => 90) then


write (*, *) "Grade is AA"
else if ( marks => 80 .and. marks < 90) then
write (*, *) "Grade is AB"
else
write (*, *) "Grade is other than AA or AB"
end if
Exercise:

1) Write the Fortran Program to compute the following formula using real variables f, x,
and y. Use a single IF THEN ELSE IF statement. You may assume the values for f, x,
and y have already been declared as real values and initialized.

2) Write the Fortran Program which will assign grades using the following marks using
IF statemts:

Marks Grade
=>90 AA
80-90 AB
70-80 BB
60-70 BC
50-60 CC
40-50 CD
<40 F

CASE Statement

A CASE statement is used to compare a given value with preselected constants and take an
action according to the first constant to match. A CASE statement can be handy to select
between a series of different possibilities or cases.
The case variable or expression must be of type integer, character, or logical. A
real type is not allowed. Based on the selector, a set of one or more of Fortran statements can
be executed.

The general format of the CASE statement is:

select case (variable)


case (selector-1)
<fortran statement(s)-1>
case (selector-2)
<fortran statement(s)-2>
...
case (selector-n)
<fortran statement(s)-n>
case default
<fortran statement(s)-default>
end select
where <fortran statement(s)-1>, <fortran statement(s)-2>, <fortran statement(s)-3>, ...,
<fortran statement(s)-n> and <fortran statement(s)-default> are sequences of one or more
executable statements.

The selector-1, selector-2, selector-3, ..., and selector-n are called selector lists. Each
CASE selector list may contain a list and/or range of integers, character or logical
constants, whose values may not overlap within or between selectors. A selector-list is either
a single or list of values, separated by commas. Each selector list must be one of the following
forms.

( value )
( value-1 : value-2 )
( value-1 : )
( : value-2 )

where value, value-1, and value-2 are constants or literals. The type of these constants must be
identical to that of the selector.
• The first form has only one value
• The second form means all values in the range of value-1 and value-2 (inclusive). In
this form, value-1 must be less than value-2
• The third form means all values that are greater than or equal to value-1
• The fourth form means all values that are less than or equal to value-2

In order, each selector expression is evaluated. If the variable value is the selector or in the
selector range, then the sequence of statements in <fortran statement(s)> are executed.
If the result is not in any one of the selectors, there are two possibilities:
• if CASE DEFAULT is there, then the sequence of statements in statements-DEFAULT
are executed, followed by the statement following END SELECT
• if the CASE DEFAULT is not there, the statement following END SELECT is executed

The constants listed in selectors must be unique. The CASE DEFAULT is optional. But with
a CASE DEFAULT, you are guaranteed that whatever the selector value, one of the labels will
be used. The place for CASE DEFAULT can be anywhere within a SELECT CASE statement;
however, putting it at the end would be more natural.

Exercise:

1) Write the Fortran Program which will assign grades using the following marks using
CASE statemts:
Marks Grade
=>90 AA
80-90 AB
70-80 BB
60-70 BC
50-60 CC
40-50 CD
<40 F

You might also like