• No results found

IF-THEN-END IF

In document Fortran Tutorial (Page 63-67)

IF-THEN-END IF

The IF-THEN-END IF form is a simplification of the general IF-THEN-ELSE-END IF form with the ELSE part omitted:

IF (logical-expression) THEN statements

END IF

where statements is a sequence of executable statements, and logical-expression is a logical expression. The execution of this IF-THEN-ELSE-END IF statement goes as follows:

the logical-expression is evaluated, yielding a logical value

if the result is .TRUE., the statements in statements are executed, followed by the statement following the

IF-THEN-END IF statement.

if the result is .FALSE., the statement following the IF-THEN-END IF is executed. In other words, if

logical-expression is .FALSE., there is no action taken.

Examples

The following program segment computes the absolute value of X and saves the result into variable Absolute_X.

Recall that the absolute value of x is x if x is non-negative; otherwise, the absolute value is -x. For example, the absolute value of 5 is 5 and the absolute value of -4 is 4=-(-4). Also note that the WRITE(*,*) statement has been intentionally broken into two lines with the continuation line symbol &. The trick is that the value of X is first saved to Absolute_X whose value is changed later only if the value of X is less than zero.

REAL :: X, Absolute_X X = ...

Absolute_X = X IF (X < 0.0) THEN Absolute_X = -X END IF

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

' is ', Absolute_X

The following program segment reads in two integer values into a and b and finds the smaller one into Smaller.

Note that the WRITE(*,*) has also been broken into two lines. This uses the same trick discussed in the previous example.

INTEGER :: a, b, Smaller READ(*,*) a, b

Smaller = a

http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap03/if-then.html (1 of 2)8/5/2006 8:04:42 PM

IF-THEN-END IF

IF (a > b) THEN Smaller = b END IF

Write(*,*) 'The smaller of ', a, ' and ', &

b, ' is ', Smaller

In many cases, it is required to do something when certain condition is satisfied; otherwise, do nothing. This is

exactly what we need the form of IF-THEN-END IF. In the following, an INTEGER variable Counter is used for counting something. When its value is a multiple of 10, a blank line is displayed.

INTEGER :: Counter

IF (MOD(Counter, 10) == 0) THEN WRITE(*,*)

END IF

A Useful Tip

The box trick can also be used with this IF-THEN-END IF form. Since there is no ELSE, you can leave the lower part empty like the following:

logical-expression

what you want to do when the logical expression is . TRUE.

nothing is here!!!

http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap03/if-then.html (2 of 2)8/5/2006 8:04:42 PM

Logical IF

Logical IF

The logical IF is the simplest form. It has the following form:

IF (logical-expression) one-statement

where one-statement is a executable statement which is not another IF, and logical-expression is a logical expression. The execution of this logical IF statement goes as follows:

the logical-expression is evaluated, yielding a logical value

if the result is .TRUE., the statement in one-statement is executed, followed by the statement following the logical

IF statement.

if the result is .FALSE., the statement following the logical IF is executed. In other words, when logical-expression

is .FALSE., there is no action taken.

Note that this logical IF does have its use although it looks not so powerful comparing with IF-THEN-ELSE-END IF and IF-THEN-END IF.

Examples

The following program segment computes the absolute value of X and saves the result into variable Absolute_X.

Recall that the absolute value of x is x if x is non-negative; otherwise, the absolute value is -x. For example, the absolute value of 5 is 5 and the absolute value of -4 is 4=-(-4). Also note that the WRITE(*,*) statement has been intentionally broken into two lines with the continuation line symbol &. The trick is that the value of X is first saved to Absolute_X whose value is changed later only if the value of X is less than zero.

REAL :: X, Absolute_X X = ...

Absolute_X = X

IF (X < 0.0) Absolute_X = -X

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

' is ', Absolute_X

The following program segment reads in two integer values into a and b and finds the smaller one into Smaller.

Note that the WRITE(*,*) has also been broken into two lines. This uses the same trick discussed in the previous example.

INTEGER :: a, b, Smaller READ(*,*) a, b

Smaller = a

IF (a > b) Smaller = b

Write(*,*) 'The smaller of ', a, ' and ', &

b, ' is ', Smaller

http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap03/if.html (1 of 2)8/5/2006 8:04:44 PM

Logical IF

In many cases, it is required to do something when certain condition is satisfied; otherwise, do nothing. This is

exactly what we need the form of IF-THEN-END IF. In the following, an INTEGER variable Counter is used for counting something. When its value is a multiple of 10, a blank line is displayed.

INTEGER :: Counter

IF (MOD(Counter, 10) == 0) WRITE(*,*)

The following is wrong since the one-statement if a logical IF can not be another IF statement.

INTEGER :: a, b, c

IF (a < b) IF (b < c) WRITE(*,*) a, b, c

From the above examples, one can easily see that if the THEN part has exactly one statement and there is no ELSE, the logical IF statement can save some space making a program a little shorter. But, my advice is that don't use it whenever it is possible.

http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap03/if.html (2 of 2)8/5/2006 8:04:44 PM

In document Fortran Tutorial (Page 63-67)

Related documents