next up previous
Next: IF Construct Up: Control Flow Previous: Control Flow

 

IF Statement

 

This is the most basic form of conditional execution in that there is only an option to execute one statement or not -- the general IF construct allows a block of code to be executed. The basic form is,

 
IF( tex2html_wrap_inline21419  logical-expression  tex2html_wrap_inline21421 ) tex2html_wrap_inline21419  exec-stmt  tex2html_wrap_inline21421 

If the boolean expression, tex2html_wrap_inline21419  logical-expression tex2html_wrap_inline21421 , evaluates to .TRUE. then the tex2html_wrap_inline21419  exec-stmt tex2html_wrap_inline21421 is executed otherwise control of the program passes to the next line. This type of IF statement still has its use as it is a lot less verbose than a block-IF with a single executable statement.

For example,

    IF (bool_val) A = 3

A logical expression is often expressed as:

 
 tex2html_wrap_inline21419  expression  tex2html_wrap_inline21421  tex2html_wrap_inline21419  relational-operator  tex2html_wrap_inline21421  tex2html_wrap_inline21419  expression  tex2html_wrap_inline21421  

For example,

    IF (bool_val) A = 3
    IF (x .GT. y) GOTO 56
    IF (i .NE. 0 .AND. j .EQ. 0) k = 1/(k*j)
    IF (i /= 0 .AND. j == 0) k = 1/(k*j) ! same
As REAL numbers are represented approximately, it makes little sense to use the .EQ. and .NE. relational operators between real-valued expressions. For example there is no guarantee that 3.0 and 1.5 * 2.0 are equal. If two REAL numbers / expressions are to be tested for equality then one should look at the size of the difference between the numbers and see if this is less than some sort of tolerance.

    IF (ABS(a-b) .LT. Tol) same = .TRUE.


next up previous
Next: IF Construct Up: Control Flow Previous: Control Flow

Adam Marshall ©University of Liverpool, 1996
Wed Oct 9 17:57:29 BST 1996