Simple IF Statements in Fortran

Fortran, one of the earliest high-level programming languages, is widely used in scientific computing, numerical analysis, and engineering simulations. Controlling the flow of execution based on conditions is a fundamental part of programming, and in Fortran, IF statements allow a program to execute a block of code only if a specified condition is true.

This post provides a comprehensive discussion of simple IF statements in Fortran, including syntax, examples with integers, real numbers, logical variables, nested IF statements, best practices, and advanced applications.

1. Introduction to IF Statements

An IF statement is a control structure that allows conditional execution of code. The program evaluates a logical expression (the condition), and if it evaluates to .true., the code inside the IF block executes. If the condition is .false., the block is skipped.

IF statements are essential for:

  • Making decisions in programs
  • Validating input
  • Controlling loops and program flow
  • Implementing algorithms with conditional logic

2. Syntax of a Simple IF Statement

The basic syntax of a simple IF statement in Fortran is:

if (condition) then
! code to execute if condition is true
end if
  • condition is a logical expression that evaluates to .true. or .false.
  • The code inside the then block executes only if the condition is true
  • The end if statement marks the end of the IF block

2.1 Example with an Integer

program simple_if_integer
integer :: num
num = 10
if (num > 0) then
    print *, "Number is positive"
end if
end program simple_if_integer

Explanation:

  • num is assigned the value 10
  • The condition num > 0 is true
  • The print statement executes, displaying “Number is positive”

2.2 Example with a Real Number

program simple_if_real
real :: x
x = -2.5
if (x < 0.0) then
    print *, "x is negative"
end if
end program simple_if_real

Explanation:

  • Real variable x is compared to 0.0
  • The IF block executes only if the condition is true

3. Using IF Statements with Logical Variables

Logical variables store .true. or .false. and can be directly used in IF statements.

program if_logical
logical :: flag
flag = .true.
if (flag) then
    print *, "Flag is true"
end if
end program if_logical

Explanation:

  • The IF statement evaluates the logical variable flag
  • Executes the block if the variable is .true.

3.1 Negating Logical Variables

You can use .not. to check if a logical variable is false:

program if_not_logical
logical :: flag
flag = .false.
if (.not. flag) then
    print *, "Flag is false"
end if
end program if_not_logical

Explanation:

  • .not. flag evaluates to .true. when flag is .false.
  • Demonstrates conditional execution based on the opposite value

4. Nested IF Statements

IF statements can be nested to evaluate multiple conditions sequentially.

program nested_if
integer :: num
num = 5
if (num > 0) then
    print *, "Number is positive"
    if (mod(num,2) == 0) then
        print *, "Number is even"
    else
        print *, "Number is odd"
    end if
end if
end program nested_if

Explanation:

  • The outer IF checks if the number is positive
  • The inner IF checks if the number is even or odd
  • Nested IF statements allow more detailed conditional logic

4.1 Nested IF with Multiple Conditions

program nested_conditions
integer :: x, y
x = 8
y = 12
if (x > 0) then
    if (y > 10) then
        print *, "x is positive and y is greater than 10"
    end if
end if
end program nested_conditions

Explanation:

  • Multiple conditions can be evaluated step by step using nested IFs
  • Useful when conditions depend on the evaluation of previous conditions

5. IF Statements with Relational Operators

Fortran allows several relational operators in IF conditions:

OperatorMeaning
==Equal to
/=Not equal to
>Greater than
<Less than
>=Greater than or equal
<=Less than or equal

Example:

program relational_if
integer :: a
a = 15
if (a &gt;= 10) then
    print *, "a is greater than or equal to 10"
end if
end program relational_if

Explanation:

  • Relational operators allow comparison of numeric values in IF statements
  • Evaluate conditions precisely

5.1 Example with NOT Equal Operator

program not_equal_if
integer :: x
x = 7
if (x /= 5) then
    print *, "x is not equal to 5"
end if
end program not_equal_if

Explanation:

  • /= operator checks inequality
  • IF block executes when the condition is true

6. IF Statements in Loops

IF statements can be combined with loops to perform conditional actions on iterations.

program if_in_loop
integer :: i
do i = 1, 10
    if (mod(i,2) == 0) then
        print *, i, "is even"
    end if
end do
end program if_in_loop

Explanation:

  • The loop iterates from 1 to 10
  • The IF statement checks each number for evenness
  • Only even numbers are printed

6.1 IF with Nested Loops

program nested_loops_if
integer :: i, j
do i = 1, 3
    do j = 1, 3
        if (i + j &gt; 4) then
            print *, "i =", i, "j =", j, "sum &gt;", 4
        end if
    end do
end do
end program nested_loops_if

Explanation:

  • Nested loops iterate over pairs of i and j
  • IF statement applies a condition on the sum of i and j

7. IF Statements with Real Numbers

IF conditions work for real numbers as well:

program if_real_numbers
real :: temp
temp = 37.5
if (temp &gt; 37.0) then
    print *, "Temperature is above normal"
end if
end program if_real_numbers

Explanation:

  • Comparison operators work for real numbers
  • Useful in simulations, temperature checks, and physical measurements

7.1 Precision Consideration

  • When using real numbers, be cautious of rounding errors
  • For equality checks with real numbers, use a tolerance:
program if_real_tolerance
real :: a, b, tol
a = 1.0/3.0
b = 0.333333
tol = 1.0e-5
if (abs(a - b) &lt; tol) then
    print *, "a and b are approximately equal"
end if
end program if_real_tolerance

Explanation:

  • Avoid direct comparison due to floating-point precision issues
  • Use a small tolerance to check approximate equality

8. IF Statements with Logical Expressions

IF conditions can combine multiple logical expressions using .and. or .or.

program if_logical_expressions
integer :: x, y
x = 12
y = 8
if (x &gt; 10 .and. y &lt; 10) then
    print *, "x is greater than 10 AND y is less than 10"
end if
end program if_logical_expressions

Explanation:

  • .and. ensures both conditions are true
  • IF block executes only when the combined logical expression is true

8.1 Using OR Operator

program if_or_example
integer :: a
a = 5
if (a &lt; 0 .or. a &gt; 10) then
    print *, "a is outside the range 0-10"
end if
end program if_or_example

Explanation:

  • .or. evaluates to true if at least one condition is true
  • IF block executes if a is negative or greater than 10

9. Best Practices for Simple IF Statements

  1. Always initialize variables to avoid undefined behavior.
  2. Use descriptive variable names for readability.
  3. Indent IF blocks properly for clarity.
  4. Use parentheses for complex conditions to avoid ambiguity.
  5. Avoid deeply nested IFs; consider using ELSE IF or SELECT CASE for clarity.
  6. Comment your conditions for maintainability, especially in scientific programs.

10. Complete Example: Simple IF Statements

program if_statements_demo
implicit none
integer :: num
real :: temp
logical :: flag
! Example 1: Integer condition
num = 10
if (num &gt; 0) then
    print *, "Number is positive"
end if
! Example 2: Real number condition
temp = 37.5
if (temp &gt; 37.0) then
    print *, "Temperature is above normal"
end if
! Example 3: Logical variable
flag = .true.
if (flag) then
    print *, "Flag is set to true"
end if
! Example 4: Nested IF
if (num &gt; 0) then
    print *, "Number is positive"
    if (mod(num,2) == 0) then
        print *, "Number is even"
    end if
end if
! Example 5: Multiple conditions
if (num &gt; 0 .and. temp &gt; 37.0) then
    print *, "Number is positive AND temperature is high"
end if
end program if_statements_demo

Explanation:

  • Demonstrates integer, real, logical conditions, nested IFs, and multiple conditions using .and.
  • Shows proper initialization and indentation for readability
  • Illustrates common use cases of IF statements in scientific programs

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *