3. If

by gg582 · 2026-05-27 05:11:26 · 22 views

Fortran 2018 Control Flow and Relational/Logical Operator Specification

This document consolidates all available if branch structures and their associated operator specifications based on the Fortran 2018 standard. It explicitly distinguishes between legacy syntax maintained for backward compatibility and modernized structures.

1. Integrated Source Code

program fortran_control_flow
    ! Load standard environment module: Define fixed precision
    use, intrinsic :: iso_fortran_env, only: wp => real64, i4 => int32
    implicit none

    ! Variable declaration and initialization
    integer(i4) :: x = 10, y = 20
    real(wp) :: a = 1.5_wp, b = 2.5_wp
    logical :: cond_a, cond_b
    character(len=10) :: trait

!!! -- Section 1: Named IF (Recommended for readability and block integrity)
    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    !! By specifying identical identifiers (labels) at the    !!
    !! start, multi-branch (else if, else), and end points,   !!
    !! the scope of the control flow is explicitly clarified. !!
    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    named_block: if (x < y .and. a /= b) then
        trait = "active"
        if (x >= 10 .or. .not. (y == 20)) then
            print *, "Section 1: Complex condition met"
        end if
    else if (x == y) then named_block
        trait = "equal"
    else named_block
        trait = "default"
    end if named_block

!!! -- Section 2: Logical IF (Single-statement structure)
    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    !! Declared when only a single executable statement       !!
    !! follows the condition. It does not construct then and  !!
    !! end if blocks, minimizing syntactic overhead.          !!
    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    cond_a = (x /= y)  ! .true.
    cond_b = (a > b)   ! .false.

    if (cond_a .neqv. cond_b) print *, "Section 2: Exclusive OR condition evaluated to true"

!!! -- Section 3: Block IF (Standard structure) & Symbolic Operators
    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    !! The most generic branching statement. Using symbolic   !!
    !! representations for relational operators is preferred. !!
    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    if (x <= y) then
        print *, "Section 3: Standard block evaluated"
    end if

!!! -- Section 4: Arithmetic IF (Obsolescent / For legacy analysis)
    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    !! *Note*: Designated as Obsolescent in the Fortran 2018  !!
    !! standard. It forces an unconditional jump (GOTO) to    !!
    !! one of three statement labels based on the sign        !!
    !! (negative, zero, positive) of the evaluation result.   !!
    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    ! If the result of (x - y) is negative -> label 100, zero -> label 200, positive -> label 300
    if (x - y) 100, 200, 300

100 print *, "Section 4: Evaluation result is negative"
    goto 400
200 print *, "Section 4: Evaluation result is zero"
    goto 400
300 print *, "Section 4: Evaluation result is positive"

400 continue

    ! Standard output execution
    write(*, '(/, A, A)') "Final Trait: ", trait
    write(*, '(A, I0, A, I0)') "Values: x=", x, ", y=", y
    write(*, '(A, F4.1, A, F4.1)') "Floats: a=", a, ", b=", b

end program fortran_control_flow


2. Control Flow Architecture Details

[1] Named Block IF

  • Characteristics: When verifying complex nested loops and multi-conditional statements, it explicitly identifies which if scope a given end if closes.
  • Control Structure: Begins with label_name: if (condition) then. The identical name must be appended to intermediate branches (else if, else) and the termination statement (end if label_name).

[2] Logical IF

  • Characteristics: Provides conciseness in terms of readability and code density when executing a single conditional operation.
  • Control Structure: Takes the form of if (condition) [single executable statement] and does not generate a structural block.

[3] Arithmetic IF

  • Characteristics: Classified as Obsolescent as of Fortran 2018 and will be entirely removed in future standards.
  • Control Structure: After the evaluation expression calculates a floating-point or integer value, execution is forced to jump to the lower label pointers depending on its sign. Since this produces spaghetti code, it must be refactored into standard Block IF structures during legacy modernization.

3. Relational and Logical Operator Specifications

Operators used for conditional expression evaluation support both modern symbolic expressions and letter-based legacy expressions.

Operator Type Modern Expression Legacy Expression Functional Definition
Relational == .eq. True if both operands are equal
/= .ne. True if operands are not equal
> .gt. True if the left operand is greater than the right
< .lt. True if the left operand is less than the right
>= .ge. True if the left operand is greater than or equal
<= .le. True if the left operand is less than or equal
Logical .and. - Logical AND (True if both conditions are true)
.or. - Logical OR (True if at least one condition is true)
.not. - Logical NOT (Reverses the truth value)
.eqv. - Logical Equivalence (True if both logical states match)
.neqv. - Logical Non-equivalence (Acts as XOR; True if states differ)
Back

Comments

No comments yet.