# Fortran: Lesson 2

#### Lesson topics

##### View Demos
# 1 # 2 # 3 # 4 # 5 # 6 # 7 # 8 # 9 # 10
##### Download Demos
# 1 # 2 # 3 # 4 # 5 # 6 # 7 # 8 # 9 # 10

We look at more of the commonly used features and commands of Fortran.

### Logical Expressions

A logical expression is a relation between variables or expressions that can have a value of TRUE or FALSE. Such expressions are used in "if Ö then" constructions and in loops, when testing whether to execute certain steps of a program. Relations and connectives appearing in logical expressions are listed in the following table; you will see how some of these are used in later examples.

 Relation/Connective Meaning .lt. less than .gt. greater than .le. less than or equal to .ge. greater than or equal to .eq. equals .ne. not equal to .and. and .or. or .not. not .xor. "exclusive" or (i.e., only one is true) .eqv. equivalent (i.e., same truth values) .neqv. not equivalent

### If ... Then ... Else Constructions

"If … Then … Else" constructions in Fortran are pretty much like those in Basic, with but a few minor modifications. First, instead of using in the tests symbols like "=", "<", ">=", etc., as in Basic, you must use the abbreviations in the preceding table. Also, tests must be enclosed in parentheses, and "else if" may be two words. Here are several examples:

1. if (x .gt. 0) print *, "x is positive"
2. if (x .ge. y .and. x .ge. z) go to 40
3. 											if (x .ge. 0) then
y = sqrt(x)
print *, y, " squared = ", x
end if
4. 											if (x .ge. 0) then
y = sqrt(x)
print *, y, " squared = ", x
else
print *, "x has no square root"
end if
5. 											if (x .ge. 0) then
print *, "x is positive"
y = sqrt(x)
else if (x .lt. 0) then
print *, "x is negative"
go to 60
else if (x .eq. 0) then
print *, "x is zero"
y = 0
end if

Observe that, as in examples 1) and 2), the one-line "if" statement does not use "then". Moreover, "else" appears on a line by itself, while "else if" shares the line with the test condition and "then".

### Stop

A stop statement stops the execution of a program. For instance, the sequence of statements below terminates the program whenever n is less than zero:

									if (n .lt. 0) then
print *, "Error - your age cannot be negative!"
stop
end if

Do not confuse stop and end. Use end only as the very last statement in the program, and use stop only to terminate the program before this last statement. Violating these rules will fatally confuse the compiler - it regards an end statement as the program's physical end.

### Labels and Go To

Labels and the "go to" statement work as in Basic, except that a label must be a number, and it must be typed in columns 2-5. Here is an example of a go to command directing the action to a labeled statement:

									if (x .lt. 0) go to 10
print *, "The square root of x is ", sqrt(x)
stop
10	print *, "x is negative and has no square root"

### Character Variables

A character variable is analogous to a string variable in Basic. A character variable must be declared at the beginning of the program, and attached to it in the declaration must be a number following an asterisk "*"; this number indicates the maximum number of symbols in the string. For example, the declaration statement
character name*20, ans*1

indicates that "name" is a character variable holding no more than 20 symbols, while "ans" is a character variable holding only one symbol.

A string in Fortran may be enclosed in either double quotes, as in "hello", or in single quotes, as in 'goodbye'.

### Do Loops

"For … Next" loops in Basic become "Do Loops" in Fortran. Such a loop begins with a do statement, and ends with either end do, or a labeled continue statement. Here are two loops that add the squares of the integers from 1 to 10:

										sum = 0
do i = 1, 10
sum = sum + i ** 2
end do
print *, "The sum is", sum
									sum = 0
do 5 i = 1, 10
sum = sum + i ** 2
5	continue
print *, "The sum is", sum

The end do and continue statements serve only to identify the end of the loop. The limits of the loop may be variables as well as numbers (e.g.: do i = m, n). As in Basic you may indicate a step size, which can be positive or negative. For example, the statement

do i = 1, 9, 2
specifies that the loop variable i run over the odd numbers 1, 3, 5, 7, 9.
Loops can be nested, and nested loops can end on the same continue statement (but not on the same end do statement). Here are two instances of nested loops assigning the entries of a 10 x 10 matrix:
										do i = 1, 10
do j = 1, 10
a(i,j) = i + j
end do
end do
									do 5 i = 1, 10
do 5 j = 1, 10
a(i,j) = i + j
5	continue