CHAPTER-1



CHAPTER-5

PROGRAM DEVELOPMENT- II

5.1 IF STRUCTURES

o IF STATEMENTS AND BLOCKS

The IF block (statement) is a conditional branch, which controls the flow of the execution of statements in a program based on some conditions. The syntax for the IF construct (the FORTRAN 95 term for the IF block) looks like:

IF (logical expression1) THEN

Block1

Elseif (logical expression2) THEN

Block2

Elseif (logical expression3) THEN

Block3

……

[ELSE

BlockE]

END IF

Where logical expression is a condition having a truth value of either true or false, and block1,block2,… and blockE are blocks of statements. If logical expression1 is true the statements in block1 are executed, and the control passes to the next statement after END IF.If logical expression1 is false, logical expression2 is evaluated. If it is true the statements in block2 are executed, followed by the next statement after END IF. If none of the conditions in the chain of IF and ELSE If statements is true then the statements in blockE are executed.

❑ There may be any number of ELSE IFs (or none at all), but there may be no more than one ELSE.

❑ Note that nothing may follow the keyword THEN on the first line of the construct.

o LOGICAL EXPRESSIONS

Logical expressions are expressions that result in one of the logical constants TRUE or FALSE. They can be formed in two ways: from numeric expressions in combination with the six relational operators (see below), or from other logical expressions in combination with logical variables and the five logical operators.

The relational operators and their meanings, with some examples, are given as follows:

_______________________________________________________________

Relational Operator Meaning Example _______________________________________________________________

.LT. or < less than A < e-5

.LE. or greater than B**2 - 4 * A*C >0

.GE. or >= greater than or equal X > = 0

• Logical Operators

____________________________________

Logical operator Precedence Meaning

____________________________________

.NOT. 1 logical negation

.AND. 2 logical intersection

.OR. 3 logical union

.eqv. and 4 logical equivalence

.neqv. and non- quivalence

• Logical variables:

These are variables that fall to the LOGICAL data type and can have 'values ' either TRUE or FALSE. Example

Real A, ABS_Value



read*, A

IF (A < 0.0) then

ABS_Value = -1.0*A

Else

ABS_Value = A

Endif

o A shorter form of the IF construct is the IF statement:

IF (logical-expression) one-statement

In such a case only a single statement is executed. The following example illustrates this.

REAL :: X, Absolute_X

X = .....

Absolute_X = X

IF (X < 0.0) Absolute_X = -X

WRITE(*,*) 'The absolute value of ', x, &

' is ', Absolute_X

o Nested IFs: When IF construct is nested, i.e. it contains another IF construct within it self, the position of the END IFs is crucial, as this determines to which IFs the END IFs belong. An ELSE IF or ELSE belongs to the most recently opened IF which has not been yet closed.

5.2 DO LOOPS

DO Loops are used to carry out repeated calculations. A situation where the number of repetitions may be determined in advance is called deterministic repetition. However, it often happens that the condition to end a repeat structure (or loop) is only satisfied during the execution of the loop itself.This type of repeat structure is called non-deterministic.

▪ Deterministic Repetition: The general syntax is given below:

DO i = j, k, n

block

END DO

This is equivalent to saying that the block between the DO and END DO statements is repeated with the number of repetitions being governed by the values of j, k, and n. J, k and n are integer expressions and i is an integer counter. On the first loop, i takes the value of j and on subsequent loops the value of the previous i is increased by n.

The following should be remembered in setting the values of j, k, and n.

• If n is positive, the block is executed with i starting at J and increased by n every time

until it has been executed for the greatest value of i not exceeding K.

• If n > o and J > K the block is not executed at all (zero-trip count)

• If n is negative, the block is executed with i starting at J and decreased by n every time until it has been executed for the smallest value of i not less than K.

• If n < 0 and J < k the block is not executed at all (zero-trip count)

o NESTED DO LOOPS

The body of a DO Loop may contain another DO loop. In this case, the second DO loop is said to be nested within the first DO loop.

▪ Non-Deterministic Loops: DO with EXIT may be used to program a non-deterministic loop

The syntax of these forms is:

DO

IF (logical-expr) EXIT

block

END DO

Or

DO

block

IF (logical-expr) EXIT

END DO

The EXIT statement is used as a means to exit from an otherwise endless loop.

A non-deterministic loop may also be programmed with a DO WHILE construct. The syntax of this structure is

DO WHILE (logical-expr)

block

END DO

3. The CASE Construct

The case construct allows selection between a number of choices or cases, based on a selector, which is the variable used for selection.

The case construct has the following general syntax:

SELECTCASE (expr)

case (selector1)

block 1

case (selector2)

block 2

[case default

block D]

where expr must be integer, character or logical. If it evaluates to a particular selector, that block is executed, otherwise CASE DEFAULT is selected, if it is present. The CAES DEFAULT does not necessarily have to be the last clause of the CASE construct. The general form of the selector is a list of non-overlapping values and ranges, of the same type as 'expr', enclosed in parentheses.

End Select

5.4 Kind Parameter

1. Each intrinsic data type (integer, real, complex, logical, and character) has a parameter, called its kind parameter, associated with it. A kind parameter is used to designate a machine representation for a particular data type. As an example, an implementation might have three real kinds, informally known as single, double precision. The kind parameters are integers and are processor dependent. Hence, kind parameters 1, 2, and 3 might be single, double, and quad precision on one system, and on a different system, kind parameters 4, 8, and 16, kind parameters 32, 64, and 128, or kind parameters 17, 3, and 25, could be used for the same things. Note that the value of the kind parameter does not necessarily have anything to do with the number of decimal digits of precision or range.

2. Each intrinsic type has a default kind. In addition, there is a second real kind that corresponds to double precision.

3. A complex value consists of two real values with the same kind; hence, the kinds used for complex values are the same as those for real values. In particular, there are complex values with real and imaginary parts that have the kind of double precision.

4. When a kind parameter is a part of another constant, it may be either an integer constant (which does not have a sign) or a named integer constant. In integer, real, and logical constants, it follows the underscore character (_) at the end. In character constants, it occurs at the front and is followed by an underscore

5. The kind of a complex constant is determined by the kinds of the two real components as follows:

o If the two parts are type integer, the kind of the complex constant is the default complex kind.

o If one part is integer and the other is real, the kind of the complex constant is the kind of the real.

o If both parts are real, the kind of the complex constant is the same as the part with the greater precision.

6. The kind intrinsic function returns the kind of its argument; the kind value is a positive integer

Syntax:

K = KIND(X)

Arguments: X shall be of type LOGICAL,INTEGER,REAL,COMPLEX or CHARACTER

Return value: The return value is a scalar of type INTEGER and of the default integer kind.

Example:

program test_kind

integer,parameter :: kc = kind(' ')

integer,parameter :: kl = kind(.true.)

print *, "The default character kind is ", kc

print *, "The default logical kind is ", kl

end program test_kind

In our compiler kind paramertes 4, 8 and 16 are used and for the above example we will get the result:

The default character kind is 4

The default logical kind is 4

If we want we can change the kind parameter of our data type by specifying the type of kind we want in bracket as shown below.

Example: integer, parameter :: short = selected_int_kind (8)

integer (kind = short) :: a,b !or

integer(kind=8)::a,b

real, parameter::R=selected_real_kind(8)

real(kind=R):: c,d !or

real (kind=8)::c,d

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download