<ASSOCIATE Example Program>

Module associate_example_module
  Type item_type
    Character(:),Allocatable :: name
    Real cost
    Integer number
  End Type
  Type(item_type),Allocatable :: stock(:)
Contains
  Subroutine create_base_stock
    stock = [ item_type('Something cheap', 1.5, 100), &
              item_type('Something expensive', 2500.17, 10), &
              item_type('Something else', 12.50, 2) ]
  End Subroutine
End Module
Program associate_example
  Use associate_example_module
  Call create_base_stock
  Do i=1,Size(stock)
    Associate(item=>stock(i), number_to_buy=>3)
      If (item%number>=number_to_buy) Then
        item%number = item%number - number_to_buy
        Print 1,number_to_buy,item%name,item%cost
1       Format(1X,'Bought ',I0,' "',A,'" at $',F0.2)
      Else
        Print 2,number_to_buy,item%name
2       Format(1X,'Could not buy ',I0,' of "',A,'"')
      End If
    End Associate
  End Do
End Program

■ Execution Results

 Bought 3 "Something cheap" at $1.50
 Bought 3 "Something expensive" at $2500.17
 Could not buy 3 of "Something else"