Manual Reference Pages  - previous (3fortran)

NAME

PREVIOUS(3) - [ENUMERATION] Previous enumeration value

SYNOPSIS

result = previous (a [, stat])

         elemental enumerator function previous(a,stat) result(answer)

enumerator,intent(in) :: a integer(kind=**),intent(out),optional :: stat enumerator :: answer

CHARACTERISTICS

o A shall be of enumeration type.
o STAT is an integer with a decimal exponent range of at least four.
o The returned value will be of the same type and kind as the argument. If A is equal to the first enumerator of its type, it is assigned

DESCRIPTION

Previous enumeration value

OPTIONS

o A : the starting value to locate the previous value relative to
o STAT : If A is equal to the last enumerator of its type, it is assigned a processor-dependent positive value; otherwise, it is assigned the value zero. If STAT would have been assigned a nonzero value but is not present, error termination is initiated.

RESULT

If A is equal to the first enumerator of its type, the value of the result is that of A. Otherwise, the value of the result is the enumerator preceding the value of A.

EXAMPLE

Example. If the enumerators of an enumeration type are EN1, EN2, EN3, and EN4, PREVIOUS (EN3) is equal to EN2, and PREVIOUS (EN1, ISTAT) is equal to EN1 and a positive value is assigned to ISTAT.

Sample program:

    program demo_previous
    implicit none

! Fortran 2023 strongly-typed enumeration enum, bind(c) :: color enumerator :: red, green, blue end enum

type(color) :: current_color

! Initialize to the first item current_color = red print *, "Initial position: ", int(current_color)

! Advance using the new NEXT intrinsic current_color = next(current_color) print *, "Next position (green): ", int(current_color)

! Advance again current_color = next(current_color) print *, "Next position (blue): ", int(current_color)

! Move backward using the new PREVIOUS intrinsic current_color = previous(current_color) print *, "Previous position (green): ", int(current_color)

end program demo_previous

Results:

     >
     >

STANDARD

Fortran 2023

SEE ALSO

o Next enumeration value: NEXT(3)
o Conversion of position to INTEGER: INT(3)
Fortran intrinsic descriptions