变量声明
变量在类型声明语句中的程序(或子程序)开头声明。
变量声明的语法如下 -
type-specifier :: variable_name
例如
integer :: total
real :: average
complex :: cx
logical :: done
character(len = 80) :: message ! a string of 80 characters
稍后您可以为这些变量赋值,例如,
total = 20000
average = 1666.67
done = .true.
message = “A big Hello from JC2182”
cx = (3.0, 5.0) ! cx = 3.0 + 5.0i
您还可以使用内在函数cmplx,为复杂变量赋值 -
cx = cmplx (1.0/2.0, -7.0) ! cx = 0.5 – 7.0i
cx = cmplx (x, y) ! cx = x + yi
例子
以下示例演示了变量声明、赋值和在屏幕上的显示 -
program variableTesting
implicit none
! declaring variables
integer :: total
real :: average
complex :: cx
logical :: done
character(len=80) :: message ! a string of 80 characters
!assigning values
total = 20000
average = 1666.67
done = .true.
message = "A big Hello from JC2182"
cx = (3.0, 5.0) ! cx = 3.0 + 5.0i
Print *, total
Print *, average
Print *, cx
Print *, done
Print *, message
end program variableTesting
编译并执行上述代码时,会产生以下结果 -
20000
1666.67004
(3.00000000, 5.00000000 )
T
A big Hello from JC2182